XML parsing using DOM parser java

I'm finding problem in parsing the xml file using the DOM parser. Where my problem is i have a parent tag called employee, where it's children node are having the same name but different attribute value. So when i run the below code it leads to the following error. Could Anyone please help me.


ERROR:

java.lang.NullPointerException
at XMLParser.getValue(XMLParser.java:53)
at XMLParser.main(XMLParser.java:30)


// this is my xml file named sample.xml
 




























// this is XMLParser.java used to parse the Sample.xml file
 
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
 
public class XMLParser {
 
public static void main(String args[]) {
try {
 
File employee = new File("Sample.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(employee);
doc.getDocumentElement().normalize();
 
System.out.println("root of xml file" + doc.getDocumentElement().getNodeName());
NodeList nodes = doc.getElementsByTagName("row");
System.out.println("==========================");
 
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
 
 
if (node.getNodeType() == Node.ELEMENT_NODE) {
        Element element = (Element) node;
        System.out.println("ID: " + getValue("ID", element));
        System.out.println("Name: " + getValue("Name", element));
        System.out.println("Gender: " + getValue("Gender", element));
        S
        }
 
}
}
 
catch (Exception ex)
{
ex.printStackTrace();
}
}
 
private static String getValue(String tag, Element element)
{
NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes();
Node node = (Node) nodes.item(0);
return node.getNodeValue();
}
}
 
 

Replies

  • Nikumbh
    Nikumbh
    Actually, i'm not able to get the children tag value. Where it's ids are same.
    ie.,







    here the
    Element element = (Element) node;

    getValue("ID", element)
    getValue("Name", element)
    getValue("Gender", element)

    fails to fetch the value.

You are reading an archived discussion.

Related Posts

hello CEans, I'm Shyam kumar completed my B.Tech 3rd year. Now as the requirement of our curriculum i'm doing a Mini-Project in Electronics Corporation of India Limited(E.C.I.L) .There i opted...
Today I was in office and around 3:00 pm, heard girls are shouting in some corner. I didn't know about it, then few minute later one guy came to my...
What Should be the distance between two electrical earth pits as per standards?
I'm sure most of us had this question before we graduated and now many more are yet to graduate. My question is: 1. How much time do you analyse that...
Many countries spends billions on space projects to other planets.But We were wasting the live Earth and trying to explore other planets for human survival.As a engineer we have a...