Whats the best way to convert XML into Java objects?

I dont 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 for XStream but didnt really like the whole move down move up type stuff...would prefer a DOM like object when writing converters...

+3  A: 

Check out the Apache Digester



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
+1  A: 

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.

+2  A: 

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.

+1  A: 

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.

+1  A: 

You can use [Castor][1]

[1]: http://www.castor.org/xml-framework.html 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.
Tushar Tarkas

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:

Blaise Doughan
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.
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
Blaise Doughan