| Author |
How to read multiple inner tag in xml in Java
|
Dinesh Pise
Greenhorn
Joined: Jul 18, 2012
Posts: 12
|
|
[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: 19216
|
|
|
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
How To Ask Questions How To Answer Questions
|
 |
Dinesh Pise
Greenhorn
Joined: Jul 18, 2012
Posts: 12
|
|
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: 32675
|
|
. . . and welcome to the Ranch
|
 |
 |
|
|
subject: How to read multiple inner tag in xml in Java
|
|
|