Ultrashort JAXB tutorial
Today I had to use JAXB for XML-object binding. While searching for an introduction, I noted that most articles handling JAXB seemed to be overly long and concerned with all kinds of asides that detracted from the basics, which were all I needed. Therefore I now present: an ultrashort JAXB tutorial.
I will expect you know:
I will expect you know:
- What XML-object binding is and why you would want to use it.
- How to use Java and solve classpath issues and such
- How to use Linux (or translate the instructions to Windows).
- How to determine intermediate steps I left out (like 'extract the zip')
- An XML file that you wish to 'unmarshal' into an object tree. Example: XML:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?xml version="1.0" encoding="UTF-8"?> <rootElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://some.sensible.url/foo" xsi:schemaLocation="http://some.sensible.url/foo foo.xsd"> <subElement> <foo>1</foo> <baz>2</baz> </subElement> <subElement> <foo>2</foo> <baz>4</baz> </subElement> </rootElement>
- An XML Schema description of the structure of the XML. Example:
XML:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<?xml version="1.0" encoding="UTF-8"?> <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://some.sensible.url/foo" xmlns:f="http://some.sensible.url/foo" elementFormDefault="qualified"> <element name="rootElement"> <complexType> <sequence> <element name="subElement" type="f:subElement" maxOccurs="unbounded" /> </sequence> </complexType> </element> <complexType name="subElement"> <sequence> <element name="foo" type="int" /> <element name="baz" type="int" /> </sequence> </complexType> </schema>
- Use a Java 6 SDK (which has JAXB) or download JAXB (and use a Java 5+ JDK)
- Generate the objects from the xml schema by issuing
jaxb-ri/bin/xjc.sh -p com.company.app.pakkage.of.objects \ -d /path/to/com/company/app/pakkage/of/objects \ /path/to/xml-structure.xsd
- Assuming the root element of your xml is named 'rootElement' (and available as com.company.app.pakkage.of.objects.RootElement):
Java:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
package com.company.app; import java.io.File; import javax.xml.bind.*; import com.company.app.pakkage.of.objects.*; public class Example { public static void main(String[] args) throws JAXBException { final JAXBContext jaxbContext = JAXBContext.newInstance("com.company.app.pakkage.of.objects"); final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); final RootElement rootElement = (RootElement) unmarshaller.unmarshal(new File("/path/to/xml.xml")); } }
- Profit.
02-'09 Is there more bad PHP than bad Java?
02-'09 More dangerous than a decline in prosperity.
Comments
4. ???
5. Profit!!!
5. Profit!!!
The
n. ???
n+1. Profit!!!
meme only lends itself when it is unclear how one may obtain profit using the process described by steps 1 .. n - 1. In this case, the ??? is obvious
.
n. ???
n+1. Profit!!!
meme only lends itself when it is unclear how one may obtain profit using the process described by steps 1 .. n - 1. In this case, the ??? is obvious

Useful link for those who want to play around with jaxb and wonder why boolean's are ****** 
http://www.mojavelinux.co...e_great_jaxb_api_blunder/

http://www.mojavelinux.co...e_great_jaxb_api_blunder/
Comments are closed