XML EXERCISES Sam Guinea [email protected] http://servicetechnologies.wordpress.com/ SAX VS DOM SAX • Simple API for XML • Event-based sequential access parser API • There is no formal definition • Only a JAVA implementation that is normative • Documents are parsed state-dependently SAX Parsers • A SAX Parser functions as a stream parser • With an event-driven API • SAX parsing is unidirectional • We define callback methods for the events • SAX events: • XML Text nodes • XML Element Starts and Ends • XML Processing Instructions • XML Comments SAX Benefits vs Drawbacks • Memory Usage is negligible • Minimum memory is proportional to the maximum depth of the XML file • Maximum data involved in a single XML event • Fastest kind of parsing for indexing, conversion, formatting, etc. • Makes it hard to do XML validation and queries (Xpath) DOM • Document Object Model • Cross-platform and language-independent • A document gains an object-based representation in memory • Nodes are accessed through object methods • Origin tied to browser wars • Fundamental piece of software for managing pages • Used a lot in Java Web service technology stack DOM Benefits vs Drawbacks • Makes it easy to manage XML files • Validation is simple • Possible to query the document • Can be slow to build the tree-like representation • Occupies a lot of memory • Can be made more efficient through persistency Let’s see some Java code… • SAX • How to read an XML file • DOM • How to read an XML file • How to modify an XML file • How to create an XML file • How to count XML elements JAXB JAXB • Java Architecture for XML Binding. • Provides a fast and convenient way to bind between XML schemas and Java representations. • Used to: • unmarshall XML instance document into Java content trees • marshall Java content trees into XML instance documents. • generate XML schemas from Java objects. JAXB Binding Process • Compiling classes. • Unmarshalling. • Generating content tree of data objects instantiated from the generated JAXB classes. • Validate both source XML documents and processed content. • Process content. • Marshalling. Representing XML Content • Grouping the generated classes in packages. • A package comprises: • A Java class name is derived from the element name. • An ObjectFactory class used to return instances of a bound Java class. • Schema 2 Java • xsd:string à java.lang.String • xsd:int à int • xsd:dateTime à javax.xml.datatype.XMLGregorianCalendar • xsd:anySimpleType à java.lang.Object @XmlRootElement @XmlRootElement( name="doc" ) public class Document { @XmlElement protected Foo foo; // ... } <?xml version="1.0" encoding="UTF-8"?> <doc> <foo>...</foo> </doc> <?xml version="1.0" encoding="UTF-8"?> <xsd:complexType name="Foo"> ... </xsd:complexType> <xsd:complexType name="Document"> <xsd:sequence> <xsd:element name="foo" type="Foo"/> </xsd:sequence> </xsd:complexType> <xsd:element name="doc" type="Document"/> @XmlType This annotation adds information that would be available from a schema type, but isn't implied by a Java class declaration. The annotation has several attributes: - The attribute name provides the XML schema name if you don't want to use the class name. - The namespace attribute provides the name of the target namespace. - The string array value defined by propOrder establishes an ordering of the sub-elements @XmlRootElement @XmlType( propOrder={ "title", "items", "cluster" } ) public class Document { ... } @XmlSchema This annotation can only be used with a package. It defines parameters that are derived from the xsd:schema element @javax.xml.bind.annotation.XmlSchema( namespace = "http://www.laune.at/hospital", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED) package hospital; <xs:schema elementFormDefault="qualified" targetNamespace="http://www.laune.at/hospital" xmlns:tns="http://www.laune.at/hospital" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0" > @XmlAccessorType If JAXB binds a class to XML, then, by default, all public members will be bound, i.e., public getter and setter pairs, or public fields. Any protected, package-visible or private member is bound if it is annotated with a suitable annotation such as XmlElement or XmlAttribute. You have several possibilities to influence this default behaviour. XmlAccessType.FIELD XmlAccessType. PROPERTY @XmlElement and @XmlAttribute @XmlElement(name = "Preamble", required = true) protected PreambleType preamble; @XmlElement(name = "Workplace", required = true) protected List<SysWorkplaceType> workplace; <xsd:sequence> <xsd:element name="Preamble" type="com:PreambleType"/> <xsd:element name="Workplace" type="SysWorkplaceType" minOccurs="1" maxOccurs="unbounded"/> </xsd:sequence> @XmlAttribute final static int answer = 42; @XmlValue public class Price { private BigDecimal amount; public Price(){} @XmlElement public BigDecimal getAmount(){ return amount; } public void setAmount( BigDecimal value ){ this.amount = value; } } <xs:complexType name="price"> <xs:sequence> <xs:element name="amount" type="xs:decimal" minOccurs="0"/> </xs:sequence> </xs:complexType> <price> <amount>123.45</amount> </price> @XmlValue public class Price { // ... @XmlValue public BigDecimal getAmount(){ return amount; } // ... } <xs:simpleType name="price"> <xs:restriction base="xs:decimal"/> </xs:simpleType> <price>123.45</price> Examples • Unmarshal Read • how to unmarshal an XML document into a Java content tree and access the data contained within it. • How to modify the data and push it back to XML • Adding XSD Validation • how to enable validation during unmarshalling. • Creating a model • Model creation and generation of an XSD • Personalization of model mapping Schema Validation boolean handleEvent(ValidationEvent event) Parameters: event - the encapsulated validation event information. It is a provider error if this parameter is null. Returns: true if the JAXB Provider should attempt to continue the current unmarshal, validate, or marshal operation after handling this warning/error, false if the provider should terminate the current operation with the appropriate. Throws: IllegalArgumentException - if the event object is null. XPATH IN JAVA JAXEN • Open source XPath library written in Java • http://jaxen.codehaus.org/ • Chosen by Sun for • JSP Standard Tag Library • Java Web services Development Pack 1.0 and 1.1 • Java API for XML Messaging • Means we can use it with SOAP messages Only three steps • Construct an XPath object by passing a String containing an XPath expression • public DOMXPath(String expression) throws JaxenException; • Set the namespace bindings by calling • public void addNamespace(String prefix, String uri) throws JaxenException; • Evaluate the expression (methods vary depending on desired result type) • public List selectNodes(Object context) throws JaxenException; LAB EXERCISES Esercizio parte 1 • Mediante annotazioni JAXB creare l’XML Schema per la gestione di una libreria (bookstore.xsd) • Bookstore • Book • Title • Author • Date • ISBN • Publisher • Cost Esercizio parte 2 • Usare lo schema “bookstore.xsd” definito al punto precedente per generare le classi del modello della libreria • Scrivere un programma che crea una libreria di 10 libri e lo serializza e salva in un file XML chiamato “books.xml” Esercizio parte 3 • Scrivere un programma che carica il contenuto del file “books.xml” in memoria sotto forma di DOM • Usare Xpath per rispondere alle seguenti query • Restituire tutti gli autori • Restituire tutti i libri scritti da un autore a vostra scelta • Restituire tutti i libri ce hanno un costo superiore a 10 euro • Restituire i nomi degli editori che pubblicano libri che costano più di 10 euro