开发者

Converting XML to Java objects [closed]

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. Closed 9 years ago. 开发者_如何学JAVA

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


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.


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.


Check out the Apache Digester

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


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:

  • Mapping File
  • Can map JPA Entities
  • XPath Based Mapping


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.


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.


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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜