aspose file tools*
The moose likes Java in General and the fly likes How to read multiple inner tag in xml in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to read multiple inner tag in xml in Java" Watch "How to read multiple inner tag in xml in Java" New topic
Author

How to read multiple inner tag in xml in Java

Dinesh Pise
Greenhorn

Joined: Jul 18, 2012
Posts: 27
[size=12]Dear Friends,

Please help me in reading multiple inner xml tags in java. Below is the example.


<PANDETAILS>
<USERID></USERID>
<MODULENAME></MODULENAME>
<PAN>
<PANNO></PANNO>
<PANNO></PANNO>
<PANNO></PANNO>
<PANNO></PANNO>
</PAN>
</PANDETAILS>

Can some please provide a code as how to read PANNO I get values of USERID and application but only one value first PANNO and then null pointer exception.

Below is the code:

NodeList nList = doc.getElementsByTagName("PANDETAILS");
// System.out.println("-----------------------");

for (int temp = 0; temp < nList.getLength(); temp++) {

Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {

Element eElement = (Element) nNode;

sModuleName = getTagValue("MODULENAME", eElement);
System.out.println(sModuleName);
sUserId = getTagValue("USERID", eElement);
System.out.println(sUserId);
// sPanNo = sPanNo+getTagValue("PANNO", eElement);
//System.out.println(sPanNo);
}
}
NodeList nList1 = doc.getElementsByTagName("PAN");
NodeList nListsize = doc.getElementsByTagName("PANNO");
String []sPanNo = new String[nListsize.getLength()];

for(int temp = 0 ; temp <nListsize.getLength(); temp++){
Node nNode = nList1.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {

Element eElement = (Element) nNode;
sPanNo [temp]= getTagValue("PANNO", eElement);
System.out.println("Pan No " +sPanNo[temp]);

}
}
[color=#444444]

Output:
CMS
1430
Pan No AMPKM3789D
java.lang.NullPointerException
at com.sbilife.nsdl.ws.ParseXML.readXML(ParseXML.java:61)
at com.sbilife.nsdl.ws.ParseXML.main(ParseXML.java:170)

Thanks![/color]
[/size]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

Can you please tell us what line 61 of your source file is? Because the exception stack trace informs us that's where the exception is thrown from, so something on that line must be null.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Dinesh Pise
Greenhorn

Joined: Jul 18, 2012
Posts: 27
Hi,

Below is code works for getting the tag value of same name for eg here it is
<PAN>
<PANNO></PANNO>
<PANNO></PANNO>
<PANNO></PANNO>
<PANNO></PANNO>
</PAN>

NodeList nPanList = doc.getElementsByTagName("PAN");


for(int temp = 0 ; temp <nPanList.getLength(); temp++){
Node nNode = nPanList.item(temp);
Element eElement = (Element) nNode;
NodeList childList = eElement.getChildNodes();
String [] sPANNO = new String[childList.getLength()] ;
for(int i = 0; i < childList.getLength(); i++){
Node childNode = childList.item(i);
sPANNO[i] = childNode.getNodeName() + "\t" + childNode.getTextContent();
System.out.println(sPANNO[i]);
}
}
Thanks!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
. . . and welcome to the Ranch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to read multiple inner tag in xml in Java