Sunday, October 27, 2013

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...
share|improve this question
 
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

closed as not constructive by casperOne Jan 9 at 18:33

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

7 Answers

I know everybody loves automatic solutions like JAXB and such, but I'd recommend hand-codingjavax.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.
share|improve this answer
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.
share|improve this answer
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:blog.bdoughan.com/2012/07/jaxb-no-annotations-required.html –  Blaise Doughan Dec 5 '12 at 11:28
Check out the Apache Digester
share|improve this answer
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:
share|improve this answer
 
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. Seebdoughan.blogspot.com/2010/07/xpath-based-mapping.html –  Blaise Doughan Jul 21 '10 at 13:07
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.
share|improve this answer
 
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 –  Piotrek De Sep 29 '11 at 12:08

No comments: