Search code examples
javaxmldomxml-parsingsax

could not read XML Values in Java


I Have an xml like below

<?xml version="1.0"?>
<class>
   <student rollno="393">
      <firstname>dinkar</firstname>
      <lastname>kad</lastname>
      <nickname>dinkar</nickname>
      <marks isNull= "85" />
      <processTimingLevel isNull= "true" />
   </student>
   <student rollno="493">
      <firstname>Vaneet</firstname>
      <lastname>Gupta</lastname>
      <nickname>vinni</nickname>
      <marks>95</marks>
       <processTimingLevel isNull="true" />
   </student>
   <student rollno="593">
      <firstname>jasvir</firstname>
      <lastname>singn</lastname>
      <nickname>jazz</nickname>
      <marks>90</marks>
       <processTimingLevel isNull="true" />
   </student>
</class>

I am trying to read all the values in it. but i could not read the values of the marks, since it is very differnt from others.

i have the below code

File inputFile = new File("input.xml");
             DocumentBuilderFactory dbFactory 
                = DocumentBuilderFactory.newInstance();
             DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
             Document doc = dBuilder.parse(inputFile);
             doc.getDocumentElement().normalize();
             System.out.println("Root element :" 
                + doc.getDocumentElement().getNodeName());
             NodeList nList = doc.getElementsByTagName("student");
             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("Student roll no : "
                            + eElement.getAttribute("rollno"));
                    System.out.println("First Name : "
                            + eElement.getElementsByTagName("firstname")
                                    .item(0).getTextContent());
                    System.out.println("Last Name : "
                            + eElement.getElementsByTagName("lastname").item(0)
                                    .getTextContent());
                    System.out.println("Nick Name : "
                            + eElement.getElementsByTagName("nickname").item(0)
                                    .getTextContent());
                    System.out.println("Marks : "
                            + eElement.getElementsByTagName("marks").item(0)
                                    .getTextContent());
                }
             }

Any help will be helpful..

I have given only a few lines of code but i have 10000 lines of code in a single xml where i have to read the values similar to the attribute like marks.

Below is the response from my console

    Root element :class
----------------------------

Current Element :student
Student roll no : 393
First Name : dinkar
Last Name : kad
Nick Name : dinkar
Marks : 

Current Element :student
Student roll no : 493
First Name : Vaneet
Last Name : Gupta
Nick Name : vinni
Marks : 

Current Element :student
Student roll no : 593
First Name : jasvir
Last Name : singn
Nick Name : jazz
Marks : 

As you can see i want to get the values of marks from xml


Solution

  • This should get you the isNull value when available, otherwise the text content:

    Element marksElm = (Element)eElement.getElementsByTagName("marks").item(0);
    String marks = marksElm.getAttribute("isNull");
    if (marks.equals("")) {
        marks = marksElm.getTextContent();
    }
    System.out.println("Marks : " + marks);