tags:

views:

57

answers:

4

I have an XML file. It could be something like:

<person>
  <name>
    <firstname>Joni</firstname>
    <lastname>Smith</lastname>
  </name>
  <born year="1983" day="31" month="01">Finland</born>
  ... lots of elements ...
</person>

My goal is to create class Person. How can I do it "automatically"? I think I have used some maven castor plugin to create a quite complicated object graph from an XML file without a lot of effort. However, I can not remember what that plugin was, and indeed can not remember how did I exactly used it. I am also very happy to learn about other (probably better) tools you might know.

+1  A: 

There a several tools. Digester, from Apache Commons, is one of them. Quite simple to use.

Update: Here's a comparison with other tools mentioned in other answers (xmlbeans, jaxb). In summary, Digester is the most thin, apt for just loading xml into objects (specially adequate for "internal" config files for example). The other tools are more oriented towards a full xml-object mapping (both directions) and specially useful when xml schemas are involved.

leonbloy
A: 

Look into JAXB. There are many tools, pick the one that best suits your needs.

dpatch
A: 

Apache XMLBeans is a very good way to generate classes from XML, and supports a lot of advanced XML features (such as type inheritance) not well supported in other tools. XMLBeans has a command line tool used to generate a jar you then include in your project which contains all the bean classes plus factories for creating and consuming XML documents.

scottw
+2  A: 

I agree with using JAXB.

Starting from XML Schema (Generate classes from XML Schema)

You can use JAXB to generate Java source code from XML Schema. Below are the instructions for doing this with EclipseLink JAXB (MOXy):

Java SE 6 comes with the Metro JAXB XJC compiler it can be found in the bin directory of your JDK installation:

C:\Program Files\Java\jdk1.6.0_20\bin>xjc -d outputDir mySchema.xsd

The Dali plug-in in Eclipse also has this support see section on JAXB class generation:

Starting from Objects

With your object model you may find the XPath based mapping extension in MOXy JAXB useful:

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Person {

    @XmlPath("name/firstname/text()")
    private String firstName;

    @XmlPath("name/lastname/text()")
    private String lastName;

    // ...
}

Can be used with the following demo code to work with your XML:

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Person.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Person person = (Person) unmarshaller.unmarshal(new File("input.xml"));

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(person, System.out);
    }
}

For more information on XPath based mappings see:

For the "born" element you might find JAXB's XmlAdapter helpful:

Blaise Doughan