XML Document. Node vs Elements, and more  
Author Message
Jbjones





PostPosted: 2004-10-31 7:28:00 Top

java-programmer, XML Document. Node vs Elements, and more I am parsing a document, so am using nodelist to get the children of
the root, and using logic to parse the Nodes depending on which one it
is. however, I need more power than "Node" can give me. So can I
somehow convert a Node to an Element (I basically need the ability to
read the attributes of an item as well as just get the value).
However, they don't have an ID that i could accurately read to use the
getElementByID() tag. So how can i convert a node to an element? Or
is there a better way for me to go about this instead of root ->
Nodelist ??

Here is some sample code:

Element docRoot= documentWx.getDocumentElement(); // get root

NodeList elements =docRoot.getChildNodes();
elements=elements.item(1).getChildNodes();
//The NodeList now holds everything under Data

for(int counter=0;counter<elements.getLength();counter++)
{
tempNode = (elements.item(counter));
Current = (Element) tempNode;
if(Current.getNodeName()=="Location")
{
//Read 'Location' , element properties of latitude, longitude
}
}

 
Sudsy





PostPosted: 2004-10-31 7:22:00 Top

java-programmer >> XML Document. Node vs Elements, and more Jbjones wrote:
> I am parsing a document, so am using nodelist to get the children of
> the root, and using logic to parse the Nodes depending on which one it
> is. however, I need more power than "Node" can give me. So can I
> somehow convert a Node to an Element (I basically need the ability to
> read the attributes of an item as well as just get the value).
> However, they don't have an ID that i could accurately read to use the
> getElementByID() tag. So how can i convert a node to an element? Or
> is there a better way for me to go about this instead of root ->
> Nodelist ??
<snip>

You know that Element is a subinterface of the Node interface, right?
And you know that you can invoke Node#getNodeType(). And you can see
from the javadocs that you can compare the result of the invocation
to Node.ELEMENT_NODE, eh? Put it all together and it should become
obvious as to how you can then cast a Node of the appropriate type
to an Element. Then do with it what you will...

--
Java/J2EE/JSP/Struts/Tiles/C/UNIX consulting and remote development.