Search code examples
javaxmlxmlreader

Read xml file with java to access every element


I have an XML file as shown below. I've created a simple program to the reader this file and print in on the console.The problem that I'm facing is that it is giving me

java.lang.NullPointerException
    at xmlreader.main(xmlreader.java:46)

and also it is not reading some values.

 <problemType>
      <fleetSize>FINITE</fleetSize>
 </problemType>
 <vehicles>
      <vehicle>
           <id>1_1</id>
           <typeId>type_1</typeId>
           <startLocation>
                <id>[x=-20.2675][y=57.4797]</id>
                <coord x="-20.2675" y="57.4797"/>
           </startLocation>
           <endLocation>
                <id>[x=-20.2675][y=57.4797]</id>
                <coord x="-20.2675" y="57.4797"/>
           </endLocation>
           <timeSchedule>
                <start>0.0</start>
                <end>1.7976931348623157E308</end>
           </timeSchedule>
           <returnToDepot>true</returnToDepot>
      </vehicle>
      <vehicle>
      </vehicles>

I've created a java piece of code to read this file

    import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;

public class xmlreader {

   public static void main(String argv[]) {

    try {

    File fXmlFile = new File("C:/Users/HP/Desktop/solution.xml");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(fXmlFile);

    //optional, but recommended
    //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
    doc.getDocumentElement().normalize();

    System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

    NodeList nList = doc.getElementsByTagName("vehicles");

    System.out.println("----------------------------");

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

        Node nNode = nList.item(temp);

        System.out.println("\nCurrent Element :" + nNode.getNodeName());

        if (nNode.getNodeType() == Node.ELEMENT_NODE) {

            Element eElement = (Element) nNode;

            System.out.println("id : " + eElement.getAttribute("id"));
            System.out.println("typeId : " + eElement.getElementsByTagName("typeId").item(0).getTextContent());
            System.out.println("startLocation : " + eElement.getElementsByTagName("startLocation").item(0).getTextContent());
            System.out.println("endLocation : " + eElement.getElementsByTagName("endLocation").item(0).getTextContent());
            System.out.println("timeSchedule : " + eElement.getElementsByTagName("timeSchedule").item(0).getTextContent());
                        System.out.println("returnTodepot : " + eElement.getElementsByTagName("returnTodepot").item(0).getTextContent());
        }
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }}

when I tried to run the program, it is giving me errors "java.lang.NullPointerException" and also it seems that it not getting the values inside startLocation tags. Please help I'm kind of confused right now


Solution

  • calling eElement.getTextContent() of returnToDepot is enough

    System.out.println(eElement.getTextContent());
    
    
    <returnToDepot>true</returnToDepot> 
    

    returnToDepot not have any child elements. But in your code ,trying to access the 0the index,Which not exists.

    eElement.getElementsByTagName("returnTodepot").item(0)