XML Data Binding
Prof. Dr. Ralf Lämmel
Universität Koblenz-Landau
Software Languages Team
What’s XML binding?
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Elevator speech
Think of a business pro...
http://xbrl.org/
Example of an XML language:
XBRL--a language for the electronic
communication of business and
financial d...
http://xbrl.org/
Objects,please!
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
What’s XML data binding?
<xml/> OOP
de-...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
101 in XML
<company>
<department deptno...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
X-to-O mapping
(as part of XML data bin...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Context: X/O/R mapping
The Bermuda Tria...
Preliminaries:
XML processing in languages for
XML query / transformation
(as opposed to OO programming languages)
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Totaling salaries
with XPath
• XPath is...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
XPath axes
AxisName Result
ancestor Sel...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Cutting salaries
with XSLT
• XSLT is an...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Cutting salaries
with XSLT
<xsl:stylesh...
Preliminaries:
XML processing with OO APIs
for XML parsing and representation
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Options for XML processing in
an OO pro...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
The DOM option
(DOM=Document Object Mod...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
What’s DOM?
• An object model for XML t...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Demo
http://101companies.org/wiki/
Cont...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
The SAX option
(SAX=Simple API for XML)...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
What’s SAX?
• A framework for event han...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Demo
http://101companies.org/wiki/
Cont...
XML schemas
XML Schema schemas
(XSDs)
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
XML languages
• Constrain the set of el...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
XML Schema (XSD)
http://www.w3.org/XML/...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
XSD for 101
	 <xs:element name="company...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
XML validation
Well-formed XML as a pre...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
XML validation options
Validate as XML ...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Demo
http://101companies.org/wiki/
Cont...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
XSD – schema components
• Element decla...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Sketch of the company schema
Exercise: ...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Model group compositors
• <sequence> ju...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
<sequence>
<xs:complexType name="employ...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
<choice>
<xs:element name="subunit">
<x...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
“*”

 <xs:element name="company">

 
 <...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Global vs. local
<xs:choice>
<xs:elemen...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
XSD simple types
• Comparable to primit...
© 2010-2013 Ralf Lämmel and quoted sources
XSD simple type system
Source: http://dret.net/lectures/xml-fall07/
152
A simple view on
XML data binding
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
O/X type mapping
<element name=“point">...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Object model for 101companies System
pu...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
XSD for 101companies System
	 <xs:eleme...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
X-to-O mapping
X-to-O
mapping
tool
XML ...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Demo
http://101companies.org/wiki/
Cont...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
XML data binding
Directions for XML dat...
The O/X
impedance mismatch
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
How to map
“s = a:x (b:y+ c:z)+ ”?
<xs:...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
How to map
“s = a:x (b:y+ c:z)+ ”?
clas...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
XML data binding is difficult because
X...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Objects are freewheeling graphs.
1
.Val...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Its also difficult because
XML trees ar...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Object graphs are edge-labeled.
1
.Valu...
Technology:JAXB
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Options for
schema-derived classes
1.PO...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Option chosen by JAXB
• POJO with field...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
POJOs
public class Company {
protected ...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Cutting salaries with JAXB
public class...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Un-/marshaling
	 public static Company ...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Annotations
@XmlAccessorType(XmlAccessT...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Liberal mapping
public class Subunit {
...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
XML data binding with JAXB
JAXB is an i...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Samples on 101
Contribution:jaxbComposi...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Summary
XML serves important scenarios....
Upcoming SlideShare
Loading in …5
×

XML data binding

960
-1

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
960
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
36
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

XML data binding

  1. 1. XML Data Binding Prof. Dr. Ralf Lämmel Universität Koblenz-Landau Software Languages Team
  2. 2. What’s XML binding?
  3. 3. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Elevator speech Think of a business programmer who wants to focus on business rules and the object model for the application. Suddenly someone drops X* word on her and she needs to send and receive messages in a XSD-ruled format. How can we make this person happy again?
  4. 4. http://xbrl.org/ Example of an XML language: XBRL--a language for the electronic communication of business and financial data
  5. 5. http://xbrl.org/ Objects,please!
  6. 6. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) What’s XML data binding? <xml/> OOP de-/serialization This is how data is represented externally for storage or exchange. This is how data is represented represented for programming.
  7. 7. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) 101 in XML <company> <department deptno="1"> <label>Leadership department</label> <manager> <name>Merlin</name> <salary>9999</salary> </manager> <member> <department deptno="11"> <label>Planning department</label> <manager> <name>Douglas</name> <salary>4200</salary> </manager> <member> <employee> <name>Peter</name> <salary>4241</salary> </employee> </member> </department> </member> . . . Think of totaling and cutting salaries for all employees. XML data binding is when such XML is represented in “business objects” and such operations are implemented in an OO manner.
  8. 8. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) X-to-O mapping (as part of XML data binding) X-to-O mapping tool XML types OO types For instance, “xjc” for Technology:JAXB of the Java platform.
  9. 9. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Context: X/O/R mapping The Bermuda Triangle of data processing Relations Objects XML In fact, there are further technical spaces. Thanks to Erik Meijer for contributing this slide or some variant thereof.
  10. 10. Preliminaries: XML processing in languages for XML query / transformation (as opposed to OO programming languages)
  11. 11. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Totaling salaries with XPath • XPath is an XML query language. • Queries are composed from query axes. • Children, Ancestors, Descendants, ... • XPath is embedded into Java et al. by APIs. • XPath queries are encoded as strings. • Examples: “//salary” “//manager/salary” All salary nodes below all manager nodes anywhere
  12. 12. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) XPath axes AxisName Result ancestor Selects all ancestors (parent, grandparent, etc.) of the current node ancestor-or-self Selects all ancestors (parent, grandparent, etc.) of the current node and the current node itself attribute Selects all attributes of the current node child Selects all children of the current node descendant Selects all descendants (children, grandchildren, etc.) of the current node descendant-or-self Selects all descendants (children, grandchildren, etc.) of the current node and the current node itself following Selects everything in the document after the closing tag of the current node following-sibling Selects all siblings after the current node namespace Selects all namespace nodes of the current node parent Selects the parent of the current node preceding Selects everything in the document that is before the start tag of the current node preceding-sibling Selects all siblings before the current node self Selects the current node “/” “//”
  13. 13. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Cutting salaries with XSLT • XSLT is an XML transformation language. • XSLT leverages XPath for node selection. • XSLT is an XML-based language. • XSLT is a functional programming language.
  14. 14. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Cutting salaries with XSLT <xsl:stylesheet> <xsl:template match="salary"> <xsl:copy> <xsl:value-of select=". div 2"/> </xsl:copy> </xsl:template> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> </xsl:stylesheet> Type-specific template Generic default Recursion into kids
  15. 15. Preliminaries: XML processing with OO APIs for XML parsing and representation
  16. 16. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Options for XML processing in an OO programming language APIs for push-based parsers Java’s SAX, ... APIs for pull-based parsers .NET’s XmlReader, Java’s StAX, ... APIs for in-memory XML trees W3C’s DOM, Java’s JDOM, .NET’s LinqToXml Programming languages with XML support VB.NET, ...
  17. 17. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) The DOM option (DOM=Document Object Model) Source: Armstrong: “Working with XML”
  18. 18. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) What’s DOM? • An object model for XML trees. • Central types: – Document – Attribute – Node • Element • Text • Comment • CData • API segments: – Construction – Navigation – Modification
  19. 19. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Demo http://101companies.org/wiki/ Contribution:dom
  20. 20. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) The SAX option (SAX=Simple API for XML) Source: Armstrong: “Working with XML”
  21. 21. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) What’s SAX? • A framework for event handling-based XML parsers. • Typical events • Open element • Close element • Find text • ...
  22. 22. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Demo http://101companies.org/wiki/ Contribution:sax
  23. 23. XML schemas XML Schema schemas (XSDs)
  24. 24. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) XML languages • Constrain the set of element tags and attributes. • Constrain the structure of elements. • Other sorts of constraints. • Thereby XML-based language are defined. • An XML processor can assume the constraints. • That is, the processor “knows what to expect”. • Type systems for valid XML: – DTD – XML Schema (XSD) – Relax NG
  25. 25. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) XML Schema (XSD) http://www.w3.org/XML/Schema: “XML Schemas express shared vocabularies and allow machines to carry out rules made by people. They provide a means for defining the structure, content and semantics of XML documents. […] XML Schema was approved as a W3C Recommendation on 2 May 2001.”
  26. 26. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) XSD for 101 <xs:element name="company"> <xs:complexType> <xs:sequence> <xs:element ref="name"/> <xs:element maxOccurs="unbounded" minOccurs="0" ref="department"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="department"> ... </xs:element> <xs:complexType name="employee"> ... </xs:complexType> XML too Grammar- like OO types- like
  27. 27. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) XML validation Well-formed XML as a prerequisite Input XML document XML schema Output “valid”: document valid w.r.t. schema “invalid” + violations Compare this to parsing with context- free grammars.
  28. 28. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) XML validation options Validate as XML document is parsed (SAX). Validate the XML document built in memory. Validate as in-memory tree is serialized. ...
  29. 29. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Demo http://101companies.org/wiki/ Contribution:sax See Company.xsd. See Validator.java. See unit test for validation.
  30. 30. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) XSD – schema components • Element declarations • Complex type definitions • Model-group definitions • Simple type definitions • Attribute declarations • Attribute-group definitions • Redefinitions • Annotations Comments and hints for schema processors Deprecated Types of leafs in XML trees (both elements and attributes). Nonrecursive macros without subtyping Recursive macros with subtyping Sets of XML trees rooted by a certain element name
  31. 31. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Sketch of the company schema Exercise: once you have seen the entire schema and completed this lecture, try to answer the following question: Why does it make (some) sense that both element declarations and complex-type definitions are put to work in the sample schema? <xs:schema …> <xs:element name="company“> … </xs:element> <xs:element name="department“> … </xs:element> <xs:complexType name="employee"> … </xs:complexType> <xs:element name="name"> … </xs:element> <xs:element name="address"> … </xs:element> <xs:element name="salary"> … </xs:element> </xs:schema>
  32. 32. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Model group compositors • <sequence> juxtaposition in EBNF • <choice> “|” in EBNF • <all> “||” (permutation phrases) • minOccurs=”0” ? • minOccurs=“1” maxOccurs=“unbounded” + • minOccurs=“0” maxOccurs=“unbounded” *
  33. 33. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) <sequence> <xs:complexType name="employee"> <xs:sequence> <xs:element ref="name" /> <xs:element ref="address" /> <xs:element ref="salary" /> </xs:sequence> </xs:complexType> An employee element has children for name, address, and salary.
  34. 34. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) <choice> <xs:element name="subunit"> <xs:complexType> <xs:choice> <xs:element name="employee" type="employee" /> <xs:element ref="department" /> </xs:choice> </xs:complexType> </xs:element> In a variation of our preferred schema, a subunit (of a department) is either an employee or a department.
  35. 35. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) “*” <xs:element name="company"> <xs:complexType> <xs:sequence> <xs:element ref="name"/> <xs:element maxOccurs="unbounded" minOccurs="0" ref="department"/> </xs:sequence> </xs:complexType> </xs:element> A company element has any number of department elements as its children.
  36. 36. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Global vs. local <xs:choice> <xs:element name="employee" type="employee" /> <xs:element ref="department" /> </xs:choice> Reference to a global element declaration Declaration of a local element declaration
  37. 37. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) XSD simple types • Comparable to primitive types in Java. • Example: <xs:element name="salary" type="xs:double"/> • There are predefined simple types in XSD. • Attributes are of simple types. • New simple types can be defined by: – Restriction – Union – List
  38. 38. © 2010-2013 Ralf Lämmel and quoted sources XSD simple type system Source: http://dret.net/lectures/xml-fall07/ 152
  39. 39. A simple view on XML data binding
  40. 40. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) O/X type mapping <element name=“point"> <complexType> <sequence> <element name="x" type=“xs:int"/> <element name="y" type="xs:int"/> </sequence> </complexType> </element> M aps to public class Point { public int x; public int y; } M aps to
  41. 41. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Object model for 101companies System public class Company { private String name; private List<Department> depts = new LinkedList<Department>(); public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Department> getDepts() { return depts; } } public class Department { ... } public class Employee { ... }
  42. 42. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) XSD for 101companies System <xs:element name="company"> <xs:complexType> <xs:sequence> <xs:element ref="name"/> <xs:element maxOccurs="unbounded" minOccurs="0" ref="department"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="department"> ... </xs:element> <xs:complexType name="employee"> ... </xs:complexType> XML too Grammar- like OO types- like
  43. 43. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) X-to-O mapping X-to-O mapping tool XML types OO types
  44. 44. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Demo http://101companies.org/wiki/ Contribution:jaxbComposition For sanity’s sake, let’s look only at Total.java and Cut.java.
  45. 45. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) XML data binding Directions for XML data binding Generate classes from XML schemas. Generate XML schemas from classes. Describe mapping only without generation. Motivations for XML data binding Support valid XML output. Hide XML in OO programming. Use XML-based object serialization.
  46. 46. The O/X impedance mismatch
  47. 47. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) How to map “s = a:x (b:y+ c:z)+ ”? <xs:element name="s"> <xs:complexType> <xs:sequence> <xs:element name="a" type="x"/> <xs:sequence maxOccurs="unbounded"> <xs:element name="b" type="y" maxOccurs="unbounded"/> <xs:element name="c" type="z"/> </xs:sequence> </xs:sequence> </xs:complexType> </xs:element>`
  48. 48. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) How to map “s = a:x (b:y+ c:z)+ ”? class s { x a; y[] b; z[] c; } Grouping of b’s and c’s is lost. This may be Ok for read access. This is not Ok for round-tripping. Occurrence constraints not enforced: Mandatory a, b, c
  49. 49. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) XML data binding is difficult because XML are parented trees whereas … <Add> <Add> <Const> element <Const> <Const> Thanks to Erik Meijer for contributing this slide or part thereof.
  50. 50. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Objects are freewheeling graphs. 1 .Value .right .left .right .left Add Add Const Thanks to Erik Meijer for contributing this slide or part thereof.
  51. 51. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Its also difficult because XML trees are node-labeled whereas … element <Add> <Add> <IConst>1 2 Thanks to Erik Meijer for contributing this slide or part thereof.
  52. 52. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Object graphs are edge-labeled. 1 .Value .right .left .right .left Const Add <Add> Const Thanks to Erik Meijer for contributing this slide or part thereof.
  53. 53. Technology:JAXB
  54. 54. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Options for schema-derived classes 1.POJO with plain fields (and getters/setters): – Requires de-/serialization library – Object state is “disconnected” from XML trees – “XML fidelity” is limited to objects as collections of fields 2.Object types that implement typed views: – Leverage XML representation: • DOM • Database – Interface provides typed access.
  55. 55. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Option chosen by JAXB • POJO with fields and getters/setters. • Annotations define XSD-related properties. • “*” and “+” are mapped to generics. • Uses of heterogeneous (weakly typed) containers: – Nested composites – Mixed context • An element factory is provided.
  56. 56. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) POJOs public class Company { protected String name; protected List<Department> department; public String getName() { return name; } public void setName(String value) { this.name = value; } public List<Department> getDepartment() { if (department == null) { department = new ArrayList<Department>(); } return this.department; } } What’s going on here?
  57. 57. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Cutting salaries with JAXB public class Cut { public static void cut(Company c) { for (Department d : c.getDepartment()) cut(d); } public static void cut(Department d) { cut(d.getManager()); for (Department s : d.getDepartment()) cut(s); for (Employee e : d.getEmployee()) cut(e); } public static void cut(Employee e) { e.setSalary(e.getSalary() / 2); } }
  58. 58. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Un-/marshaling public static Company readCompany(File input) throws JAXBException { JAXBContext jaxbContext = JAXBContext.newInstance("org.softlang.company"); Unmarshaller unMarshaller = jaxbContext.createUnmarshaller(); return (Company)unMarshaller.unmarshal(input); }
  59. 59. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Annotations @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { "name", "department" }) @XmlRootElement(name = "company") public class Company { @XmlElement(required = true) protected String name; protected List<Department> department; ... }
  60. 60. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Liberal mapping public class Subunit { protected Employee employee; protected Department department; public Employee getEmployee() { return employee; } public void setEmployee(Employee value) { this.employee = value; } public Department getDepartment() { return department; } public void setDepartment(Department value) { this.department = value; } } <xs:element name="subunit"> <xs:complexType> <xs:choice> <xs:element name="employee" type="employee"/> <xs:element ref="department"/> </xs:choice> </xs:complexType> </xs:element> What is (too) liberal here?
  61. 61. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) XML data binding with JAXB JAXB is an integral part of the Java SDK since Java 6. Source: http://java.sun.com/developer/technicalArticles/WebServices/jaxb/
  62. 62. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Samples on 101 Contribution:jaxbComposition Contribution:jaxbChoice Contribution:jaxbExtension Contribution:jaxbSubstitution
  63. 63. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Summary XML serves important scenarios. Data exchange and storage Language-independent data model Semi-structured data XML isn’t optimal. It is complex, in fact, a job-security technology. Fortunately, there are alternatives: JSON, JAXB, ... XML programming could be simpler. Manageability of XML data binding varies. Looks seductively simple at first. Becomes more complex eventually. O/X impedance mismatch to be resolved.
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×