SlideShare una empresa de Scribd logo
1 de 63
Descargar para leer sin conexión
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 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/> OOP
de-/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
tool
XML types
OO types
For instance, “xjc”
for Technology:JAXB
of the Java platform.
(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.
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 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 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
“/” “//”
(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.
(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 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, ...
(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 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 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 document
XML 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 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">
<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
(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 mapping
X-to-O
mapping
tool
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 because
XML 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 because
XML 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
Const
Add
<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)
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?
(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);
	 }	 	
}
(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:jaxbComposition
Contribution:jaxbChoice
Contribution:jaxbExtension
Contribution:jaxbSubstitution
(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.

Más contenido relacionado

Destacado

Surfacing ‘101’ in a Linked Data manner as presented at SATToSE 2013
Surfacing ‘101’ in a Linked Data manner as presented at SATToSE 2013Surfacing ‘101’ in a Linked Data manner as presented at SATToSE 2013
Surfacing ‘101’ in a Linked Data manner as presented at SATToSE 2013Ralf Laemmel
 
Language processing patterns
Language processing patternsLanguage processing patterns
Language processing patternsRalf Laemmel
 
Selected design patterns (as part of the the PTT lecture)
Selected design patterns (as part of the the PTT lecture)Selected design patterns (as part of the the PTT lecture)
Selected design patterns (as part of the the PTT lecture)Ralf Laemmel
 
Functional data structures
Functional data structuresFunctional data structures
Functional data structuresRalf Laemmel
 
Remote method invocation (as part of the the PTT lecture)
Remote method invocation (as part of the the PTT lecture)Remote method invocation (as part of the the PTT lecture)
Remote method invocation (as part of the the PTT lecture)Ralf Laemmel
 
Modeling software systems at a macroscopic scale
Modeling software systems  at a macroscopic scaleModeling software systems  at a macroscopic scale
Modeling software systems at a macroscopic scaleRalf Laemmel
 

Destacado (6)

Surfacing ‘101’ in a Linked Data manner as presented at SATToSE 2013
Surfacing ‘101’ in a Linked Data manner as presented at SATToSE 2013Surfacing ‘101’ in a Linked Data manner as presented at SATToSE 2013
Surfacing ‘101’ in a Linked Data manner as presented at SATToSE 2013
 
Language processing patterns
Language processing patternsLanguage processing patterns
Language processing patterns
 
Selected design patterns (as part of the the PTT lecture)
Selected design patterns (as part of the the PTT lecture)Selected design patterns (as part of the the PTT lecture)
Selected design patterns (as part of the the PTT lecture)
 
Functional data structures
Functional data structuresFunctional data structures
Functional data structures
 
Remote method invocation (as part of the the PTT lecture)
Remote method invocation (as part of the the PTT lecture)Remote method invocation (as part of the the PTT lecture)
Remote method invocation (as part of the the PTT lecture)
 
Modeling software systems at a macroscopic scale
Modeling software systems  at a macroscopic scaleModeling software systems  at a macroscopic scale
Modeling software systems at a macroscopic scale
 

Similar a XML Data Binding Explained

Metaprograms and metadata (as part of the the PTT lecture)
Metaprograms and metadata (as part of the the PTT lecture)Metaprograms and metadata (as part of the the PTT lecture)
Metaprograms and metadata (as part of the the PTT lecture)Ralf Laemmel
 
Generative programming (mostly parser generation)
Generative programming (mostly parser generation)Generative programming (mostly parser generation)
Generative programming (mostly parser generation)Ralf Laemmel
 
Database programming including O/R mapping (as part of the the PTT lecture)
Database programming including O/R mapping (as part of the the PTT lecture)Database programming including O/R mapping (as part of the the PTT lecture)
Database programming including O/R mapping (as part of the the PTT lecture)Ralf Laemmel
 
Functional OO programming (as part of the the PTT lecture)
Functional OO programming (as part of the the PTT lecture)Functional OO programming (as part of the the PTT lecture)
Functional OO programming (as part of the the PTT lecture)Ralf Laemmel
 
Multithreaded programming (as part of the the PTT lecture)
Multithreaded programming (as part of the the PTT lecture)Multithreaded programming (as part of the the PTT lecture)
Multithreaded programming (as part of the the PTT lecture)Ralf Laemmel
 
Notes from the Library Juice Academy courses on XPath, XSLT, and XQuery: Univ...
Notes from the Library Juice Academy courses on XPath, XSLT, and XQuery: Univ...Notes from the Library Juice Academy courses on XPath, XSLT, and XQuery: Univ...
Notes from the Library Juice Academy courses on XPath, XSLT, and XQuery: Univ...Allison Jai O'Dell
 
eXtensible Markup Language (XML)
eXtensible Markup Language (XML)eXtensible Markup Language (XML)
eXtensible Markup Language (XML)Serhii Kartashov
 
XRX Presentation to Minnesota OTUG
XRX Presentation to Minnesota OTUGXRX Presentation to Minnesota OTUG
XRX Presentation to Minnesota OTUGOptum
 
Approaches to document/report generation
Approaches to document/report generation Approaches to document/report generation
Approaches to document/report generation plutext
 
Do it on your own - From 3 to 5 Star Linked Open Data with RMLio
Do it on your own - From 3 to 5 Star Linked Open Data with RMLioDo it on your own - From 3 to 5 Star Linked Open Data with RMLio
Do it on your own - From 3 to 5 Star Linked Open Data with RMLioOpen Knowledge Belgium
 
Xml and xml processor
Xml and xml processorXml and xml processor
Xml and xml processorHimanshu Soni
 
Xml and xml processor
Xml and xml processorXml and xml processor
Xml and xml processorHimanshu Soni
 
Machine-Interpretable Dataset and Service Descriptions for Heterogeneous Data...
Machine-Interpretable Dataset and Service Descriptions for Heterogeneous Data...Machine-Interpretable Dataset and Service Descriptions for Heterogeneous Data...
Machine-Interpretable Dataset and Service Descriptions for Heterogeneous Data...andimou
 
XML and Related Technologies - Web Technologies (1019888BNR)
XML and Related Technologies - Web Technologies (1019888BNR)XML and Related Technologies - Web Technologies (1019888BNR)
XML and Related Technologies - Web Technologies (1019888BNR)Beat Signer
 
OPP2010 (Brussels) - Programming with XML in PL/SQL - Part 2
OPP2010 (Brussels) - Programming with XML in PL/SQL - Part 2OPP2010 (Brussels) - Programming with XML in PL/SQL - Part 2
OPP2010 (Brussels) - Programming with XML in PL/SQL - Part 2Marco Gralike
 
XML, XML Databases and MPEG-7
XML, XML Databases and MPEG-7XML, XML Databases and MPEG-7
XML, XML Databases and MPEG-7Deniz Kılınç
 

Similar a XML Data Binding Explained (20)

Metaprograms and metadata (as part of the the PTT lecture)
Metaprograms and metadata (as part of the the PTT lecture)Metaprograms and metadata (as part of the the PTT lecture)
Metaprograms and metadata (as part of the the PTT lecture)
 
Generative programming (mostly parser generation)
Generative programming (mostly parser generation)Generative programming (mostly parser generation)
Generative programming (mostly parser generation)
 
Database programming including O/R mapping (as part of the the PTT lecture)
Database programming including O/R mapping (as part of the the PTT lecture)Database programming including O/R mapping (as part of the the PTT lecture)
Database programming including O/R mapping (as part of the the PTT lecture)
 
Functional OO programming (as part of the the PTT lecture)
Functional OO programming (as part of the the PTT lecture)Functional OO programming (as part of the the PTT lecture)
Functional OO programming (as part of the the PTT lecture)
 
Multithreaded programming (as part of the the PTT lecture)
Multithreaded programming (as part of the the PTT lecture)Multithreaded programming (as part of the the PTT lecture)
Multithreaded programming (as part of the the PTT lecture)
 
Notes from the Library Juice Academy courses on XPath, XSLT, and XQuery: Univ...
Notes from the Library Juice Academy courses on XPath, XSLT, and XQuery: Univ...Notes from the Library Juice Academy courses on XPath, XSLT, and XQuery: Univ...
Notes from the Library Juice Academy courses on XPath, XSLT, and XQuery: Univ...
 
Microservices in Clojure
Microservices in ClojureMicroservices in Clojure
Microservices in Clojure
 
eXtensible Markup Language (XML)
eXtensible Markup Language (XML)eXtensible Markup Language (XML)
eXtensible Markup Language (XML)
 
Unit 10: XML and Beyond (Sematic Web, Web Services, ...)
Unit 10: XML and Beyond (Sematic Web, Web Services, ...)Unit 10: XML and Beyond (Sematic Web, Web Services, ...)
Unit 10: XML and Beyond (Sematic Web, Web Services, ...)
 
XRX Presentation to Minnesota OTUG
XRX Presentation to Minnesota OTUGXRX Presentation to Minnesota OTUG
XRX Presentation to Minnesota OTUG
 
Approaches to document/report generation
Approaches to document/report generation Approaches to document/report generation
Approaches to document/report generation
 
Do it on your own - From 3 to 5 Star Linked Open Data with RMLio
Do it on your own - From 3 to 5 Star Linked Open Data with RMLioDo it on your own - From 3 to 5 Star Linked Open Data with RMLio
Do it on your own - From 3 to 5 Star Linked Open Data with RMLio
 
Agile xml
Agile xmlAgile xml
Agile xml
 
Xml and xml processor
Xml and xml processorXml and xml processor
Xml and xml processor
 
Xml and xml processor
Xml and xml processorXml and xml processor
Xml and xml processor
 
Machine-Interpretable Dataset and Service Descriptions for Heterogeneous Data...
Machine-Interpretable Dataset and Service Descriptions for Heterogeneous Data...Machine-Interpretable Dataset and Service Descriptions for Heterogeneous Data...
Machine-Interpretable Dataset and Service Descriptions for Heterogeneous Data...
 
XML and Related Technologies - Web Technologies (1019888BNR)
XML and Related Technologies - Web Technologies (1019888BNR)XML and Related Technologies - Web Technologies (1019888BNR)
XML and Related Technologies - Web Technologies (1019888BNR)
 
OPP2010 (Brussels) - Programming with XML in PL/SQL - Part 2
OPP2010 (Brussels) - Programming with XML in PL/SQL - Part 2OPP2010 (Brussels) - Programming with XML in PL/SQL - Part 2
OPP2010 (Brussels) - Programming with XML in PL/SQL - Part 2
 
Xml 215-presentation
Xml 215-presentationXml 215-presentation
Xml 215-presentation
 
XML, XML Databases and MPEG-7
XML, XML Databases and MPEG-7XML, XML Databases and MPEG-7
XML, XML Databases and MPEG-7
 

Último

Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 

Último (20)

Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 

XML Data Binding Explained

  • 1. XML Data Binding Prof. Dr. Ralf Lämmel Universität Koblenz-Landau Software Languages Team
  • 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. http://xbrl.org/ Example of an XML language: XBRL--a language for the electronic communication of business and financial data
  • 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. (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. (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. (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. Preliminaries: XML processing in languages for XML query / transformation (as opposed to OO programming languages)
  • 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. (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. (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. (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. Preliminaries: XML processing with OO APIs for XML parsing and representation
  • 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. (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. (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. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Demo http://101companies.org/wiki/ Contribution:dom
  • 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. (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. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Demo http://101companies.org/wiki/ Contribution:sax
  • 23. XML schemas XML Schema schemas (XSDs)
  • 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. (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. (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. (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. (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. (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. (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. (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. (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. (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. (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. (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. (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. (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. © 2010-2013 Ralf Lämmel and quoted sources XSD simple type system Source: http://dret.net/lectures/xml-fall07/ 152
  • 39. A simple view on XML data binding
  • 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. (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. (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. (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. (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. (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.
  • 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. (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. (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. (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. (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. (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.
  • 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. (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. (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. (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. (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. (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. (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. (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. (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. (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.