Ultrashort JAXB tutorial

By Confusion on Tuesday 03 February 2009 20:49 - Comments (3)
Categories: Java, Software engineering, XML, Views: 6.315

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:
  • 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')
Requirements:
  • 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>
Tutorial
  1. Use a Java 6 SDK (which has JAXB) or download JAXB (and use a Java 5+ JDK)
  2. 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
  3. 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[] argsthrows JAXBException {
            
            final JAXBContext jaxbContext =
                JAXBContext.newInstance("com.company.app.pakkage.of.objects");
            final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            final RootElement rootElement =
                (RootElementunmarshaller.unmarshal(new File("/path/to/xml.xml"));
        }
    }
  4. Profit.

Volgende: Is there more bad PHP than bad Java? 02-'09 Is there more bad PHP than bad Java?
Volgende: More dangerous than a decline in prosperity. 02-'09 More dangerous than a decline in prosperity.

Comments


By Tweakers user ViperNL, Tuesday 03 February 2009 23:07

4. ???
5. Profit!!!

By Tweakers user Confusion, Wednesday 04 February 2009 21:55

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 :).

By Tweakers user Kettrick, Thursday 05 February 2009 21:38

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/

Comments are closed