views:

392

answers:

5

Hello, I have a String which contains XML nodes within it, and am seeking to use DOM parsing to process this string to extract node values and store them in local variables.

The XML which is stored in a String variable:

<carGarage>
   <car>
      <make>Chrysler</make>
      <color>Red</color>
   </car>
   <car>
      <make>Musano</make>
      <color>Blue</color>
   </car>
</carGarage>

My Java class where I want to extract the XML values and store them in the local attributes:

public class CarGarage
{   String make, color;

    public void setMake(String make)
    { this.make = make; }

    public void setColor(String color)
    { this.color = color; }

    public void DOMparsingMethod(Node thisNode)
    { int typeOfNode = thisNode.getNodeType();
      ...
    }
}

What is the best way to approach this? I have looked at StringBufferInputStream but that is deprecated. I am truly quite lost.

Thankyou, Lucas.

+1  A: 

Look at using DocumentBuilder to parse from any kind input stream. Here's an example that reads to a class like yours from a file.

justinhj
Hi Justin,I have been using DocumentBuilderFactory and Document Builder, but when I print the value out, it get [#DOCUMENT: NULL]...could this be because I am not returning a document element node, but the first child underneath it, in this case, the <carGarage> tag?
Lycana
Do you have <?xml version="1.0"?> at the top? Maybe it's just not finding any data due to that.
justinhj
+1  A: 

There are lots of object-to-xml binding libraries out there which do exactly what you want, but they tend to be rather bulky tools.

For this simple example, handling the dom yourself makes sense. justinhj's suggestion of the built-in java libraries for this is a good start, although this sometimes gets ugly too, since the jdk doesn't usually provide you with an xml parser, requiring you to plug in your own magically behind the scenes.

I tend to prefer jdom for this sort of thing. It's effectively the same as the DocumentBuilder route, but similar and only partly compatible.

Jeremy Huiskamp
+1  A: 

Why use DOM?

If you are trying to just read in and turn the xml into an object then I would suggest STAX, as SAX is faster than DOM, but too much coding, STAX is much nicer, and you can learn more about it below. http://blogs.techrepublic.com.com/programming-and-development/?p=639

James Black
Personally I'd go with SAX (http://xerces.apache.org/xerces-j/), because I'm used to it, and I don't think SAX is too much coding, because it's relatively simple code, and unlike stax, it's schema agnostic code (for whatever it's worth).
corlettk
I used SAX for years, but I found Stax simpler, so I tend to prefer that, as, for sax I wrote my own code to handle everything.
James Black
A: 

In my experience DOM is useful for this kind of thing because it has a low learning curve compared to SAX / STAX, even though its not as fast or memory efficient. Once you've got a DOM document, you can use XPath queries against the document to get individual element contents and parse them.

Jherico
A: 

I suggest you look at XStream. It is support conversion of XML into objects. You can give it the XML and it will give you a list of the objects you want.

Peter Lawrey