shiroe
shiroe

Reputation: 617

Select all children of a node using DOM

I have this XML code:

<root>
  <node>
    </first_child>
    </second_child>
    </third_child>
  </node>
</root>

I need to take all children nodes one by one and save like three Node variable using DOM.

If I use

doc.getElementsByTagName("node");

I take this "node" with all the children, while I need only "first_child, second_child and third_child"

How to obtain this?

Upvotes: 0

Views: 3128

Answers (3)

Suzan Cioc
Suzan Cioc

Reputation: 30097

Element el;
el = (Element) doc.getElementsByTagName("node").item(0);
el.getChildNodes();

Upvotes: 1

poplitea
poplitea

Reputation: 3727

Element el = (Element)(doc.getElementsByTagName("node").item(0));
NodeList children = el.getChildNodes();

for (int i=0; i<children.getLength(); i++) {
  System.out.println(children.item(0).getNodeValue());
}

Upvotes: 2

arelangi
arelangi

Reputation: 325

You can get the children in this way.

var children = document.getElementById('node').getElementsByTagName('*');

Upvotes: 0

Related Questions