• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to read multiple inner tag in xml in Java

 
Dinesh Pise
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
Pie
Posts: 20493
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dinesh Pise
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48381
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic