22

What's the best way to convert XML into Java objects?

I don't want a like for like representation, but would like to pull out certain data from the XML and populate a Java object. I had a look at XStream, but didn't really like the whole "move down, move up" type of stuff. I would prefer a DOM like object when writing converters...

Pabru
  • 231
  • 2
  • 7
DD.
  • 21,498
  • 52
  • 157
  • 246
  • Xstream looks fine the only thing is that I would rather work with the JDOM implementation of HierarchicalStreamReader....I guess I could cast it but this seems a bit strange....Is there a way to use converters but using JDOM reader? – DD. Jul 18 '10 at 16:16

7 Answers7

24

If you have an XML schema , JAXB is nice - comes as part of the JDK. Generate java classes by running e.g. xjc -p foo myschema.xsd

To read an XML file and get back an object (from classes generated by the xjc tool):

    JAXBContext context = JAXBContext.newInstance(FooObj.class);
    Unmarshaller unMarshaller = context.createUnmarshaller();
    FooObj param = (FooObj) unMarshaller.unmarshal(new FileInputStream("Foo.xml"));

You can do similar things if you only want parts of an XML document converted to an object, you should e.g. be able to give JAXB part of a DOM document, instead of a whole file as done above.

nos
  • 223,662
  • 58
  • 417
  • 506
  • 1
    +1 - You can also use JAXB (JSR-222) implementations when starting from Java classes. You only need to add annotations where you want the XML representation to differ from the default: http://blog.bdoughan.com/2012/07/jaxb-no-annotations-required.html – bdoughan Dec 05 '12 at 11:28
12

I know everybody loves automatic solutions like JAXB and such, but I'd recommend hand-coding javax.xml.bind.Marshaller and javax.xml.bind.Unmarshaller implementations to get exactly what you want without filling up your perm gen space unnecessarily. Use JDOM to parse XML and map the values into Java objects using XPath. It'll be some work to do it once, but you'll have exactly what you need and nothing more.

duffymo
  • 305,152
  • 44
  • 369
  • 561
8

Check out the Apache Digester

http://commons.apache.org/digester/

lucas1000001
  • 2,740
  • 1
  • 25
  • 22
6

JAXB is the best way to convert objects to XML, and MOXy is the best JAXB implementation.

  • JAXB is standard, included in Java SE 6
  • Standard XML binding technology for JAX-WS, JAX-RS, and SCA
  • Simple runtime

MOXy offers the following extensions:

bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • I want to go the other way from XML to an object. Also my XML document is very large and I just want to parse a small subset of the data into my objects. – DD. Jul 20 '10 at 23:58
  • 1
    JAXB/MOXy also supports XML to an object. You only need to map the content you want everything else will be ignored. MOXy's XPath based mapping helps you keep a small object model. See http://bdoughan.blogspot.com/2010/07/xpath-based-mapping.html – bdoughan Jul 21 '10 at 13:07
4

You can use Castor for convenient conversion of XML to Java and back.

With Castor, you can

  1. Ease conversion: A simple Marshaller and Unmarshaller makes conversion of XML to Java and back extremely easy.
  2. Mapping: A mapping file can be created to restrict the amount of data to be converted from XML to Java and back.
informatik01
  • 16,038
  • 10
  • 74
  • 104
Tushar Tarkas
  • 1,592
  • 13
  • 25
  • Castor mapping files are easy and intuitive however: * you can pass value as constructor parameter only if it is xml attribute (elements as constructor parameters are not supported) * you cannot write directly to the private fields so if you have domain implemented in the DDD-approach (no setters and constructors instead) and input xml store information as elements, there is no way to use castor – omnomnom Sep 29 '11 at 12:08
3

I like Simple and have found it very easy to work with. It does produce a like for like representation though so maybe not quite what you are looking for.

If you are looking for a DOM-like solution maybe a JDom would be suitable.

Mark
  • 28,783
  • 8
  • 63
  • 92
2

I'm not entirely sure if this is what you are looking for, but you can use something like XMLBeans to bind XML to Java objects. I had to use it at a previous employer. Unfortunately, it was an existing system and I only had to manipulate the objects and didn't have to produce the library that contains them. Also, I'm not sure how well it will work without an XSD (you didn't mention if you had one or not).

Thomas Owens
  • 114,398
  • 98
  • 311
  • 431