File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get nested elements using DOM

 
Rajendar Goud
Ranch Hand
Posts: 220
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 220
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 183
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic