• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

How to read multiple inner tag in xml in Java

 
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]
 
Sheriff
Posts: 21758
102
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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!
 
Marshal
Posts: 64618
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!