Documente Academic
Documente Profesional
Documente Cultură
Architecture For
XML Binding (JAXB)
By:
Yoav Zibin
Sharon Krisher
I wish to write
returns a string
root.getAddress().getNumber()
returns a number
JAXB solution:
Mapping XML Schema to Java interfaces
Binding Compiler
Source
schema
Java
interface
s
<xs:complexType name="AddressType">
<xs:sequence>
<xs:element name="Number" type="xs:unsignedInt"/>
<xs:element name="Street" type="xs:string"/>
</xs:sequence>
</xs:complexType>
Binding Compiler
public interface AddressType {
long getNumber();
void setNumber(long value);
String getStreet();
void setStreet(String value);
}
Must be non-negative
Must be non-null
Talk Outline
Main Features
Unmarshal: xml objects
Create / Read / Update / Delete objects
Validate objects
Marshal: objects xml
No roundtrip guarantees
Must be non-negative
String getStreet();
void setStreet(String value);
}
Must be non-null
Must be non-null
unmarshaller.setValidating(true);
PersonType person =
(PersonType) unmarshaller.unmarshal(
new FileInputStream("demo.xml") );
Step 6: Read
System.out.println("Person name=" +
person.getName() );
AddressType address = (AddressType)
person.getAddress().get(0);
System.out.println("First Address: " +
" Street=" + address.getStreet() +
" Number=" + address.getNumber() );
addressList.clear();
output.xml
<Person>
<Name>Yoav Zibin</Name>
<Address>
<Street>Hanoter</Street>
<Number>5</Number>
</Address>
</Person>
Similar Technologies
Relaxer
Castor.org
Naming
Java Properties
Simple and Complex Types
Validation Constraints
Validation
Marshal:
marshaller.marshal(Object, OutputStream)
must be an element,
otherwise the resulting
output is not a legal XML
General content
<xs:any/>
Object getAny();
void setAny(Object elementOrValue);
Naming
Class Name
Method Name
mixedCaseName
MixedCaseName
getMixedCaseName
name-with-dash
NameWithDash
getNameWithDash
aa_bb-cc
AaBbCc
getAaBbCc
Java Properties
List property
java.util.List getAddress();
In Java1.5:
List<AddressType>
Indexed Property
List getAny();
Primtive
ID/IDREF
date
integer
List
Union
List
Next (1)
String/Object
Calendar
Restriction
Represented as
validation
constraints
maxInclusive
BigInteger
Enumeration
int
int
Next (2)
Common supertype of
(Integer, String) is
Object
ComplexType
SimpleContent ComplexContent
*Extension
*Sequence * Choice
Extension
The interface
extends the base
types interface
All
Elements
Attributes
Restriction
( ) Next
Represented as
a Java interface
use
default
fixed
abstract
nillable
minOccurs
maxOccurs
Represented as
Java properties
*
*
Complex Types
Represented as a Java interface
Anonymous type
An interface is created.
The name is derived from the name of the
schema element + Type, e.g Foo FooType
interface InternationalPrice {
int getValue();
void setValue(int);
String getCurrency();
void setCurrency(String);
<xs:complexType name="Foo">
<xs:sequence>
<xs:element ref="A"/>
<xs:element ref="B"/>
<xs:element ref="C"/>
</xs:sequence>
</xs:complexType>
interface Foo {
int getA(); void setA(int);
int getB(); void setB(int);
int getC(); void setC(int);
}
</xs:sequence>
XML fragment
</xs:complexType>
Dear Mr.<name>Robert Smith</name>,
interface lb
LetterBody
{
LetterBody
= ObjectFactory.createLetterBody();
interface
Name extends Element {
List gcl
= lb.getContent();
String
getValue();
gcl.add("Dear
Mr.");
void setValue(String);
gcl.add(ObjectFactory.createLetterBodyName("Robert
Smith"));
}
The programmer
is responsible to
only set one of
Foo or Bar
When maxOccurs>1
<xs:complexType name="FooBarType">
<xs:choice maxOccurs="unbounded">
<xs:element name="Foo" type="xs:int"/>
<xs:element name="Bar" type="xs:string"/>
</xs:choice>
</xs:complexType>
XML
Objects
in
memory
marshal
Nillable elements
<xs:element name=age" type=xs:int" nillable="true"/>
Integer getAge();
void setAge(Integer value);
Java:
or
setAge(null)
XML:
<age>25</age>
or
<age xsi:nil="true"/>
Customization
Used to augment the default mapping
Customizations declared via special xml
tags
May appear inside the source schema or
in a separate file
Uses of Customization
Weak Typing
Our suggestions:
Marshal / Unmarshal : Object Element
IDREF: Object Identifiable
obj.setA(5);
obj.setB(a);
What happens
when we marshal
obj?
No roundtrip
42true
obj.setFoo(42);
obj.unsetAge();
obj.setBar("A");
System.out.println(
obj.getAge() );
);
System.out.println(
obj.isSetFoo()
The END
Any questions?