aspose file tools*
The moose likes XML and Related Technologies and the fly likes How to get nested elements using DOM Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "How to get nested elements using DOM" Watch "How to get nested elements using DOM" New topic
Author

How to get nested elements using DOM

Rajendar Goud
Ranch Hand

Joined: Mar 06, 2002
Posts: 220
Hi ,
Iam using Dom and trying to retrieve values of nested elements.can i have some example on it ?

ie
<Parent>
<Node>
<ChildNode>value</ChildNode>
<Node>
</parent>


How to read the element 'ChildNode' and retrieve its value.

Thanks
Raj
David Patterson
Ranch Hand

Joined: Jul 01, 2002
Posts: 65
With DOM processing, you get a Document at the start.

From the Document, here is how you find the element you want.


The value of ChildNode is stored in a Text node. The code above should return the correct value for you.

Dave Patterson
Rajendar Goud
Ranch Hand

Joined: Mar 06, 2002
Posts: 220
Thanks Dave,

But still am not able to get the thing running. i am totally new to xml and doesnt know much details of xml methods.
I have the following method,which is capturing only the 'nodes' which are NOT 'nested'.What modifications does it require,if it needs to read in the nested nodes existing for a particular Node.

public static Hashtable simpleAttrHash(Node _xml)
{
Hashtable outputHash = new Hashtable();
NodeList elements = _xml.getChildNodes();
Node target = null;
String val = null;
for(int i = 0; i < elements.getLength(); i++)
{
target = elements.item(i);
if(target.getNodeType() != 1)
continue;
NodeList children = target.getChildNodes();
if(children == null || children.getLength() != 1)
continue;
Node child = children.item(0);
if(child.getNodeType() == 3)
{
val = child.getNodeValue();
outputHash.put(target.getNodeName(), val);
}
}

return outputHash;
}


and the xml structure is as follows .

<node>
<E1>Element1</E1>
<subNode>
<SE1>Subelement1</SE1>
<SE2>Subelement2</SE2>
</subNode>
</node>

am not able to read 'SE2' using the above method.
can anyone pls help out with the above method ?

Thanks,
Raj
Rajagopal Manohar
Ranch Hand

Joined: Nov 26, 2004
Posts: 183
Tip 1: Whitespaces and newlines are also Nodes
Tip 2: I think you ought to use some form of recursion fo your method to work
David Patterson
Ranch Hand

Joined: Jul 01, 2002
Posts: 65
As was suggested, use recursion.

Write a method that takes an Element as a parameter. Its job is to
1) get the child nodes of that Element.
2) loop or iterate through the nodes
3) if you find another Element, check to see if it has elements as children or Text as a child. (If it has Elements, it may also have as its first child a Text node with a newline. So, don't just check for first child == Text as the sole decision.)
4) Add the elements with values to your hashmap,
5) Call the same method to process the child Elements of the new Element.

Also, it is VERY BAD FORM to use code like:



I've been doing Java processing of XML for about 3 years, and I have no idea what type of node you are selecting. If you want to use code like this, at least use the static fields in Node like Node.ELEMENT_NODE. Another approach is to use



The third approach works if you know you will not be seeing processing instructions or other strange content - just Elements, Text nodes and Comment nodes.



Dave Patterson
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get nested elements using DOM