JAXB Transparency No. 1 JAXB (Java API for XML Data Binding) Cheng-Chia Chen.
XML data binding
-
Upload
ralf-laemmel -
Category
Technology
-
view
828 -
download
2
Transcript of XML data binding
XML Data BindingProf. Dr. Ralf Lämmel
Universität Koblenz-LandauSoftware 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 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?
http://xbrl.org/
Example of an XML language: XBRL--a language for the electronic
communication of business and financial data
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/> OOPde-/serialization
This is how data is represented externally for
storage or exchange.
This is how data is represented represented for
programming.
(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.
(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
toolXML types
OO types
For instance, “xjc” for Technology:JAXBof the Java platform.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Context: X/O/R mappingThe 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.
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 salarieswith 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
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
XPath axesAxisName Resultancestor 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 nodedescendant 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 nodepreceding 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
“/” “//”
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Cutting salarieswith 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.
(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
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 programming language
APIs for push-based parsersJava’s SAX, ...
APIs for pull-based parsers.NET’s XmlReader, Java’s StAX, ...
APIs for in-memory XML treesW3C’s DOM, Java’s JDOM, .NET’s LinqToXml
Programming languages with XML supportVB.NET, ...
(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”
(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
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Demo
http://101companies.org/wiki/Contribution:dom
(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”
(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• ...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Demo
http://101companies.org/wiki/Contribution:sax
XML schemasXML Schema schemas
(XSDs)
(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
(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.”
(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
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
XML validation
Well-formed XML as a prerequisite Input
XML documentXML schema
Output“valid”: document valid w.r.t. schema“invalid” + violations
Compare this to parsing with context-
free grammars.
(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....
(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.
(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
(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>
(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” *
(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.
(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.
(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.
(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
(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
© 2010-2013 Ralf Lämmel and quoted sources
XSD simple type systemSource: http://dret.net/lectures/xml-fall07/
152
A simple view onXML data binding
(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>
Maps to public class Point { public int x; public int y; }
Maps to
(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 { ... }
(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
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
X-to-O mappingX-to-O
mappingtool
XML types
OO types
(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.
(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.
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: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>`
(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
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
XML data binding is difficult becauseXML are parented trees whereas …
<Add>
<Add> <Const>
element
<Const><Const>
Thanks to Erik Meijer for contributing this slide or part thereof.
(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.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Its also difficult becauseXML trees are node-labeled whereas …
element
<Add>
<Add> <IConst>1 2
Thanks to Erik Meijer for contributing this slide or part thereof.
(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
ConstAdd
<Add>
Const
Thanks to Erik Meijer for contributing this slide or part thereof.
Technology:JAXB
(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.
(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.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
POJOspublic 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?
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Cutting salaries with JAXBpublic 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); } }
(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); }
(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; ...}
(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?
(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/
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Samples on 101
Contribution:jaxbCompositionContribution:jaxbChoiceContribution:jaxbExtensionContribution:jaxbSubstitution
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
SummaryXML serves important scenarios.
Data exchange and storageLanguage-independent data modelSemi-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.