Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you want to start coding Java to XML and XML to Java in less than 5 minutes, try Simple XML Serialization. Don't spend hours learning the JAXB API <a href="http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php" rel="nofollow noreferrer"> <a href="http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php" rel="nofollow noreferrer">http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php</a></a></p> <p>However, if you are really keen on learning JAXB, here's an excellent tutorial <a href="http://blogs.oracle.com/teera/entry/jaxb_for_simple_java_xml" rel="nofollow noreferrer"><a href="http://blogs.oracle.com/teera/entry/jaxb_for_simple_java_xml" rel="nofollow noreferrer">http://blogs.oracle.com/teera/entry/jaxb_for_simple_java_xml</a></a></p> <p>Contents of tutorial:</p> <p><strong>JAXB for simple Java-XML serialization</strong></p> <p>There're a number of way to do XML serialization in Java. If you want fine-grained control over parsing and serialization you can go for SAX, DOM, or Stax for better performance. Yet, what I often want to do is a simple mapping between POJOs and XML. However, creating Java classes to do XML event parsing manually is not trivial. I recently found JAXB to be a quick and convenient Java-XML mapping or serialization.</p> <p>JAXB contains a lot of useful features, you can check out the reference implementation here. <a href="https://www.java.net/blogs/kohsuke/" rel="nofollow noreferrer">Kohsuke's Blog</a> is also a good resource to learn more about JAXB. For this blog entry, I'll show you how to do a simple Java-XML serialization with JAXB.</p> <p><strong>POJO to XML</strong></p> <p>Let's say I have an Item Java object. I want to serialize an Item object to XML format. What I have to do first is to annotate this POJO with a few XML annotation from javax.xml.bind.annotation.* package. See code listing 1 for Item.java</p> <p>From the code</p> <ul> <li><code>@XmlRootElement(name="Item")</code> indicates that I want to be the root element.</li> <li><code>@XmlType(propOrder = {"name", "price"})</code> indicates the order that I want the element to be arranged in XML output.</li> <li><code>@XmlAttribute(name="id", ...)</code> indicates that id is an attribute to root element.</li> <li><code>@XmlElement(....)</code> indicates that I want price and name to be element within Item.</li> </ul> <p>My <code>Item.java</code> is ready. I can then go ahead and create JAXB script for marshaling Item.</p> <pre><code>//creating Item data object Item item = new Item(); item.setId(2); item.setName("Foo"); item.setPrice(200); ..... JAXBContext context = JAXBContext.newInstance(item.getClass()); Marshaller marshaller = context.createMarshaller(); //I want to save the output file to item.xml marshaller.marshal(item, new FileWriter("item.xml")); </code></pre> <p>For complete code Listing please see Code Listing 2 <code>main.java</code>. The output Code Listing 3 <code>item.xml</code> file is created. It looks like this:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt; &lt;ns1:item ns1:id="2" xmlns:ns1="http://blogs.sun.com/teera/ns/item"&gt; &lt;ns1:itemName&gt;Foo&lt;/ns1:itemName&gt; &lt;ns1:price&gt;200&lt;/ns1:price&gt; &lt;/ns1:item&gt; </code></pre> <p>Easy right? You can alternatively channel the output XML as text String, Stream, Writer, ContentHandler, etc by simply change the parameter of the marshal(...) method like</p> <pre><code>... JAXBContext context = JAXBContext.newInstance(item.getClass()); Marshaller marshaller = context.createMarshaller(); // save xml output to the OutputStream instance marshaller.marshal(item, &lt;java.io.OutputStream instance&gt;); ... JAXBContext context = JAXBContext.newInstance(item.getClass()); Marshaller marshaller = context.createMarshaller(); StringWriter sw = new StringWriter(); //save to StringWriter, you can then call sw.toString() to get java.lang.String marshaller.marshal(item, sw); </code></pre> <p><strong>XML to POJO</strong></p> <p>Let's reverse the process. Assume that I now have a piece of XML string data and I want to turn it into Item.java object. XML data (Code listing 3) looks like</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt; &lt;ns1:item ns1:id="2" xmlns:ns1="http://blogs.sun.com/teera/ns/item"&gt; &lt;ns1:itemName&gt;Bar&lt;/ns1:itemName&gt; &lt;ns1:price&gt;80&lt;/ns1:price&gt; &lt;/ns1:item&gt; </code></pre> <p>I can then unmarshal this xml code to Item object by</p> <pre><code>... ByteArrayInputStream xmlContentBytes = new ByteArrayInputStream (xmlContent.getBytes()); JAXBContext context = JAXBContext.newInstance(Item.getClass()); Unmarshaller unmarshaller = context.createUnmarshaller(); //note: setting schema to null will turn validator off unmarshaller.setSchema(null); Object xmlObject = Item.getClass().cast(unmarshaller.unmarshal(xmlContentBytes)); return xmlObject; ... </code></pre> <p>For complete code Listing please see Code Listing 2 (main.java). The XML source can come in many forms both from Stream and file. The only difference, again, is the method parameter:</p> <pre><code>... unmarshaller.unmarshal(new File("Item.xml")); // reading from file ... // inputStream is an instance of java.io.InputStream, reading from stream unmarshaller.unmarshal(inputStream); </code></pre> <p><strong>Validation with XML Schema</strong></p> <p>Last thing I want to mention here is validating input XML with schema before unmarshalling to Java object. I create an XML schema file called item.xsd. For complete code Listing please see Code Listing 4 (Item.xsd). Now what I have to do is register this schema for validation.</p> <pre><code>... Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI) .newSchema(new File("Item.xsd")); unmarshaller.setSchema(schema); //register item.xsd shcema for validation ... </code></pre> <p>When I try to unmarshal XML data to POJO, if the input XML is not conformed to the schema, exception will be caught. For complete code Listing please see Code Listing 5 (invalid_item.xml).</p> <pre><code>javax.xml.bind.UnmarshalException - with linked exception: javax.xml.bind.JAXBException caught: null [org.xml.sax.SAXParseException: cvc-datatype-valid.1.2.1: 'item1' is not a valid value for 'integer'.] </code></pre> <p>Here I change the 'id' attribute to string instead of integer.</p> <p>If XML input is valid against the schema, the XML data will be unmarshalled to Item.java object successfully.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload