Please Help !!! XML beginner using Xerces

Please Help!!! I am using xerces to try to turn an xml structure into a java object. I am getting a Class Cast Exception that I can't seem to get around. Also, I think my approach to this is very wrong. There is too much code to handle this simple task. Is there a bettter way?

something like:

<message>

<to>Joe</to>

<from>Harry</from>

</message>

Mapping to:

class MyMessage {

String to;

String from;

public MyMessage(String to, String from){...}

}

So far the following code works to parse. But how do I get the text "to" and "from"?

// This block of code works fine...

String xmlString; // Contains xmlMessage...

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

DocumentBuilder builder = factory.newDocumentBuilder();

Document doc = builder.parse(new InputSource(

new(StringReader(xmlString));

// This block of code doesn't...

DocumentImpl impl = (DocumentImpl)doc;

// Throws ClassCastException ? Why is this ?

// This used to work.

// Is there an easier way to do this?

String toTxt, fromTxt;

Element root = doc.getDocumentElement();

Element toEl = (Element)root.getElementsByTagName("to").item(0);

NodeList list = toEl.getChildNodes();

for(int i = 0; i<list.length; i++) {

Node node = list.item(i);

if(node instanceof Text)

{

toTxt = node.getNodeValue();

}

}

// Same with "from";

// now I can build my object

MyMessage m = new MyMessage(toTxt, fromTxt);

Why would I get a ClassCastException? If I can just get the DocumentImpl. I could search nodes until I find my "to" and "from" strings? How are you supposed to get a DocumentImpl from a document?

Also, Isn't there an easier way to get the relevant information? All I am interested in is the text between ><to></to> and <from></from>?

Is that supposed to be so much work?

Please help.

Tracy

[2143 byte] By [tmonteit] at [2007-9-26 1:44:57]
# 1

Well, org.w3c.dom.Document is an interface and DocumentImpl is a class that implements that interface. So, that's why you get a ClassCastException. Don't worry, you don't need the cast.

A nice thing is, that the interface Document extends the interface Node. So, you can use the methods in Node here as well (instead of Element in your code). In other words, Document is also a Node and you can view Document as being the root node of the parsed document and you can use a Document type where a Node type is required.

Thus, simply doc.getElementsByTagName("testNode") gives you a NodeList containing all the Nodes whose name is "testNode" (collected from all over the document).

If you know, that there's exactly one "to"-node in your source, containing only text you want to retrieve, instead of

// Is there an easier way to do this?

String toTxt, fromTxt;

Element root = doc.getDocumentElement();

Element toEl = (Element)root.getElementsByTagName("to").item(0);

NodeList list = toEl.getChildNodes();

for(int i = 0; i<list.length; i++) {

Node node = list.item(i);

if(node instanceof Text)

{

toTxt = node.getNodeValue();

}

you can simply write:

toTxt=doc.getElementsByTagName("to").item(0).getFirstChild().getNodeValue();

Note, that if your xml data does not contain a "to" Element, doc.getElementsByTagName would return null, so referencing item(0) would reference a null pointer and cause a nullPointerException.

So, if you always can rely on the fact that you always have one "to" element and one "from" element in your string, you could simply write:

MyMessage m = new MyMessage(

doc.getElementsByTagName("to").item(0).getFirstChild().getNodeValue(),

doc.getElementsByTagName("from").item(0).getFirstChild().getNodeValue()

);

Take a look at xerces' Document, NodeList and Node documentation for further help.

Hope that helps,

Good luck.>

lk555 at 2007-6-29 2:40:54 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...