0

What would be the best way to process a complex xml file in java?

Something like this:

<company>
<owner>
   <ownername></ownername>
   <ownerage></ownerage>
</owner>

<cars>
   <car>
      <carname></carname>
      <engines></engine>
       <user>
          <name></name>
          <age></age>
          <address></address> 
      </user>
   </car>
</cars>
</company>

What would be the best way to extract car or user?

Cœur
  • 34,719
  • 24
  • 185
  • 251
karq
  • 799
  • 2
  • 12
  • 26
  • if you got your answer accept the one that helped you the most so others can learn. – RMT Jul 06 '11 at 01:46

6 Answers6

3

Jaxb is a really good way of parsing XML. It is fairly simple to use.

Also you can use XPath to navigate through XML.

RMT
  • 6,920
  • 4
  • 24
  • 37
2

Check out jaxb. I believe it is now the offical java xml serialization library. Given you have a xsd for your xml, jaxb will generate the needed java classes based off of that schema. From there you can convert back and fourth between xml and java objects relatively painlessly.

One thing to keep in mind that I always forget is the terms marshalling and unmarshalling

java objects -> xml  = marshalling
xml -> java objects = unmarshalling
Scott
  • 10,846
  • 10
  • 49
  • 83
  • 1
    +1 - JAXB (JSR-222) is the Java standard for object-to-XML mapping. There are several implementations: Metro (the RI included in Java SE 6), EclipseLink MOXy (I'm the tech lead), Apache JaxMe, etc: http://bdoughan.blogspot.com/2010/07/jaxb-xml-binding-standard.html – bdoughan Jun 13 '11 at 15:45
  • I dont have a xsd schema file but I have a dtd file. Will that work? – karq Jun 13 '11 at 18:31
  • Yes, but it is experimental http://jaxb.java.net/2.1.2/docs/vendorSchemaLangs.html – Scott Jun 13 '11 at 18:59
  • You don't require an XML schema, you can start with an existing domain model and apply annotations to customize the mapping to XML (http://wiki.eclipse.org/EclipseLink/Examples/MOXy/GettingStarted). Here is another example of using JAXB with a DTD: http://bdoughan.blogspot.com/2011/05/jaxb-and-dtd.html – bdoughan Jun 13 '11 at 20:02
1

If you're allowed to play with other JVM languages the Groovy support for XML is excellent. http://groovy.codehaus.org/Reading+XML+using+Groovy's+XmlSlurper

Otherwise, this article has a decent introduction to the whole shebang: http://java.sun.com/developer/technicalArticles/xml/JavaTechandXML/

Drew
  • 14,640
  • 15
  • 66
  • 77
1

Depends how complex. JAXB maps the elements and attributes to Java classes and properties. It becomes cumbersome if the XML is really complex because you end up with thousands of classes, but it's very neat if the XML is relatively simple. For many tasks loading the data into a tree model such as JDOM or XOM and then querying it using XPath is the simplest approach. (In my view, avoid DOM itself, it has too much legacy.)

Michael Kay
  • 147,186
  • 10
  • 83
  • 148
  • WRT JAXB, starting from an XML schema there will be approximately one Java class generated per complex type in the XML schema. Starting from Java classes you can reduce the number of classes using mechanisms such as `@XmlElementWrapper`. – bdoughan Jun 13 '11 at 19:14
1

If you are doing to use JAXB it is handy to generate a schema first from some sample data.

Another approach would be to use XStream which is more efficient but also more work.

Peter Lawrey
  • 513,304
  • 74
  • 731
  • 1,106
0

uhmm My recommendation it's that you need make a lit be of search in the web, see the libraries and examples to complete you doubt. Can start with dom4j jdom and so many more. So your question depend's about how library you used

Jorge
  • 17,164
  • 18
  • 79
  • 125