How to read the xml tags in java

tomba2k

New Member
Hi I have an xml as shown below\[code\]<?xml version="1.0" encoding="UTF-8" standalone="yes"?><A> <B> <X1> <Name>ZZZsaqw</Name> </X1> <X1> <Name>ZZZsdasda</Name> </X1> <X1> <Name>ZZZsdsd</Name> </X1> <S>17000</S> <U>18000</U> <V>17000</V> <B> <C> <X1> <Name>ZZZasqw</Name> </X1> <X1> <Name>ZZZsdsd</Name> </X1> <X1> <Name>ZZ</Name> </X1> <S>17000</S> <U>18000</U> <V>17000</V> <C> <D> <X1> <Name>ZZZx</Name> </X1> <X1> <Name>ZZZzz</Name> </X1> <X1> <Name>ZZZsaa</Name> </X1> <S>17000</S> <U>18000</U> <V>17000</V> </D><A>\[/code\]I am interested in the X1 tag. I need to read them respective of their parent tags which can be either B, C or D. So at times I am interested only in X1 tags belonging to B sometimes belonging to C. How to do this? I used the DOM parser shown below but when I am counting them, it is showing the count as 9 where as it should have been 3. Please help me how to do it.\[code\] NodeList plist = doc.getElementsByTagName("A"); System.out.println("The length of A is pList is:"+plist.getLength()); //get the contents of the A for (int temp = 0; temp < plist.getLength(); temp++) { Node nNode = plist.item(temp); if (nNode.getNodeType() == Node.ELEMENT_NODE) { //Element eElement = (Element) nNode; NodeList typicalCriticalPath = doc.getElementsByTagName("B"); System.out.println("The length of B is:"+typicalCriticalPath.getLength()); //get the contents of the typical critical path for(int temp1 = 0; temp1<typicalCriticalPath.getLength();temp1++) { Node typCriticalPathNode = typicalCriticalPath.item(temp); if (typCriticalPathNode.getNodeType() == Node.ELEMENT_NODE) { Element ele = (Element) typCriticalPathNode; NodeList planItemSection = doc.getElementsByTagName("X1"); System.out.println("The length of X1 is:"+planItemSection.getLength()); } } } } //The output for this is like this- //The length of A is pList is:1 //The length of B is:1 //The length of X1 is:9\[/code\]Can someone please point me where I am going wrong? What is to be done so that I can get the tags wrt parent tag only.Thanks!
 
Back
Top