how to get all roots of a child in xml using java -


i have xml file below.

<root>  <subchilda>     <childa>          <dataa>data</dataa>          <datab>data</datab>     </childa>  </subchilda>  <subchildb>     <childb>          <dataa>data</dataa>          <datab>data</datab>     </childb>  </subchildb> </root> 

i can node values eg(dataa or datab 's value). need roots of data nodes.

eg dataa in childa->subchilda->root. using java documentbuilder reading.

edit

using below code can recursively read nth child cant roots.

public static void readnode( node node) {         string id;         nodelist sublist = node.getchildnodes();         (int j = 0; null != sublist && j < sublist.getlength(); j++) {             node tmpnode = sublist.item(j);             if (tmpnode.haschildnodes()) {                 element element = (element) tmpnode;                 id = element.getattribute("id");                      system.out.println( id + ":"                             + tmpnode.gettextcontent());                  readnode( tmpnode);             }         }     } 

you have traverse xml backwards reach root node.

using node node.getparentnode() method can parent node of current node , apply same on parent node until reach required node(parent).


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -