xml parsing using jaxb

22
XML Parsing Using JAXB

Upload: malintha-adikari

Post on 23-Jan-2018

673 views

Category:

Software


0 download

TRANSCRIPT

Page 1: XML parsing using jaxb

XML Parsing Using JAXB

Page 2: XML parsing using jaxb
Page 3: XML parsing using jaxb

● Object (DOM, JDOM….etc)

● PUSH (SAX)

● PULL(Stax)

● JAXB(Java Architecture for XML Binding)

Page 4: XML parsing using jaxb

● Defines an API for reading and writing Java objects to and from XML documents.

● Don't need to be aware of XML parsing techniques● No extra dependency needed (its inside JDK)● Used as a standard in many cases

XML JAXB Java Objects

Page 5: XML parsing using jaxb

Page 6: XML parsing using jaxb
Page 7: XML parsing using jaxb
Page 8: XML parsing using jaxb

JAXB uses annotations to indicate the central elements.

Annotation Definition

@XmlRootElement(namespace = "namespace") Define the root element for an XML tree

@XmlType(propOrder = { "field2", "field1",.. }) Allows to define the order in which the fields are written in the XML file

@XmlElement Define the XML element which will be used.

@XmlAttribute Define the attribute of an element

Page 9: XML parsing using jaxb

Marshalling UnMarshalling

Page 10: XML parsing using jaxb

XML :

<?xml version="1.0" encoding="UTF-8"?> <booking xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <company name="ACME Consulting"> <address>10 Coyote Avenue, Arizona, USA</address> <contact name="Duke" email="[email protected]" telephone="1234567890"/> </company> <student firstName="Jane" surname="Dow"/> <student firstName="John" surname="Doe"/> </booking>

XSD :

<?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="booking" type="courseBooking"/> <xsd:complexType name="courseBooking"> <xsd:sequence> <xsd:element ref="company" /> <xsd:element ref="student" minOccurs="1" maxOccurs="unbounded"/> </xsd:sequence> <xsd:attribute name="courseReference" type="xsd:string" use="required"/> <xsd:attribute name="courseDate" type="xsd:date" use="required"/> <xsd:attribute name="invoiceReference" type="xsd:string" use="required"/> <xsd:attribute name="totalPrice" type="xsd:decimal" use="required"/> </xsd:complexType> <xsd:element name="student" type="studentType"/> <xsd:complexType name="studentType"> <xsd:attribute name="firstName" type="xsd:string" use="required"/> <xsd:attribute name="surname" type="xsd:string" use="required"/> </xsd:complexType> <xsd:element name="company" type="companyType"/> <xsd:complexType name="companyType"> <xsd:sequence> <xsd:element name="address"/> <xsd:element ref="contact" /> </xsd:sequence> <xsd:attribute name="name" type="xsd:string"/> </xsd:complexType> <xsd:element name="contact" type="contactType"/> <xsd:complexType name="contactType"> <xsd:attribute name="name" type="xsd:string" use="required"/> <xsd:attribute name="telephone" type="xsd:string" use="required"/> <xsd:attribute name="email" type="xsd:string" use="required"/> </xsd:complexType> </xsd:schema>

Page 11: XML parsing using jaxb

run the following command:

$xjc course-booking.xsd -p org.wso2.jaxb.training -d src/generated

● -d <dir>: Place the generated files into this directory.● -p <package>: Place the generated files in this package.● -nv: Don't perform strict validation of the input schema.● -httpproxy <proxy>: Use this if you are behind a proxy. Takes the format [user[:password]@]proxyHost[:proxyPort].● -classpath <arg>: Specify the classpath, if necessary.● -readOnly: Generates read-only source code files, if your OS supports this.

There is also an equivalent ant task, which makes it quite easy to integrate into an Ant or Maven-based build process.

Page 12: XML parsing using jaxb
Page 13: XML parsing using jaxb
Page 14: XML parsing using jaxb
Page 15: XML parsing using jaxb
Page 16: XML parsing using jaxb
Page 17: XML parsing using jaxb
Page 19: XML parsing using jaxb

JAXB advantageso It is very simple to use than DOM or SAX

parsero We can marshal XML file to other data

targets like inputStream,URL,DOM node.o We can unmarshal XML file from other

data targets.o We don't need to be aware of XML parsing

techniques.o We don't need to access XML in tree

structure always.

JAXB disadvantageso JAXB is high layer API so it has less

control on parsing than SAX or DOM.o It has some overhead tasks so it is slower

than SAX.

Page 20: XML parsing using jaxb

Questions?