开发者

How to create object graph from an XML-file?

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.


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

  • http://wiki.eclipse.org/EclipseLink/Examples/MOXy/JAXB/Compiler

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:

  • http://www.eclipse.org/webtools/releases/3.2.0/NewAndNoteworthy/jpa.php

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:

  • http://bdoughan.blogspot.com/2010/09/xpath-based-mapping-geocode-example.html

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

  • http://bdoughan.blogspot.com/2010/07/xmladapter-jaxbs-secret-weapon.html


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜