Ultrashort JAXB tutorial

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

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[] 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"));
        }
    }

  4. Profit.

More dangerous than a decline in prosperity.

By Confusion on Monday 2 February 2009 20:48 - Comments (4)
Category: Politics, Views: 3.265

We've been hearing about the financial crisis for over a year now, but I don't think many people have tried to imagine what we may be in for. The expected decline in prosperity may be the least of our concerns, when articles like these get published and linked on Slashdot.

In the article, indignation is expressed over the fact that over 21800 requests for immigration permits were issued by 'the same banks that are now receiving rescue packages totaling over $150 billion, even as the system was melting down'.

Factually, the article is ludicrous. Firstly the number '21800' is the total over the past six years, which certainly doesn't match up with 'even as the system was melting down'. Secondly, that number is pointless without an estimate of the number of people working for the corporations that issued the requests. When you have that estimate, you will note that the 3700 requests a year is actually quite small when compared to the total number of jobs in the sector and when comparing it with other Western countries, it turns out to be a pretty average number. Thirdly, the article goes on to complain about the average salary these immigrants receive, comparing it to the average salary of an american household, while completely ignoring the fact that we are talking about people in banking here. I could go on, but I think I made my point.

As this article obviously isn't intended to be informative, what is its intention? I can see only one. Mix the anger people feel towards the banking sector with their latent xenophobia and their natural suspicion of the government and you've got an article that pushes people towards nationalism. This is the kind of inflammatory 'journalism' that may well be a signpost of things to come: in the coming crisis every country is on its own and as nationalistic tendencies awaken, we may well be in for xenophobia and war.