Getting different xml children

I have an xml file that looks like this.

<Device>
<Staff>
<Name>ABC</Name>
<Name>Hello</Name>
</Staff>
<Connect>
<Speed>123</Speed>
<Speed>456</Speed>
</Connect>
</Device>

      

I need help getting the name and speed value as I have never tried xml before. I am getting a null pointer exception whenever I try to get the values ​​of the elements. Any help is appreciated.

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      DocumentBuilder builder = factory.newDocumentBuilder();

      // Load the input XML document, parse it and return an instance of the
      // Document class.
      Document document = builder.parse(new File("C:/Users/AA/Desktop/eclipse/lol/testing.xml"));//change to own directory


      NodeList nodeList = document.getDocumentElement().getChildNodes();
      System.out.println(nodeList.getLength());
     for (int i = 0; i < nodeList.getLength(); i++) {
           Node node = nodeList.item(i);

           if (node.getNodeType() == Node.ELEMENT_NODE) {
               System.out.println(i);
                Element elem = (Element) node;

                // Get the value of the ID attribute.
             //   String ID = node.getAttributes().getNamedItem("ID").getNodeValue();

                // Get the value of all sub-elements.
                String name = elem.getElementsByTagName("Name")
                                    .item(0).getChildNodes().item(0).getNodeValue();

               Integer speed = Integer.parseInt(elem.getElementsByTagName("Connect")
                        .item(0).getChildNodes().item(0).getNodeValue());//null pointer exception happens here

                staffList.add(new staff(name));
                connectList.add(new connect(speed));
           }
      }

      // Print all employees.
      for (staff stl : staffList)
           {System.out.println("STAFF "+stl.getName());}
      for (connect ctl : connectList)
      {System.out.println("Connect "+ctl.getSpeed());}

      

+3


source to share


2 answers


You will have null pointer exceptions because you assume that on each iteration of the loop, the for

required nodes have children:

String name = elem.getElementsByTagName("Name")
                                .item(0).getChildNodes().item(0).getNodeValue();

      

In the above code, you are accessing the first child of the element Name

, which is the text node (for example ABC

), and then getting its child nodes, which will throw an exception since there are no children of the elements inside the text node.

Similarly,

Integer speed = Integer.parseInt(elem.getElementsByTagName("Connect")
                    .item(0).getChildNodes().item(0).getNodeValue());

      



will result in an exception in one of the loop iterations where it elem

matches itself Connect

.

You can try the following code instead:

if (node.getNodeType() == Node.ELEMENT_NODE) {
    System.out.println(i);
    Element elem = (Element) node;

    // Get the value of the ID attribute.
    // String ID =
    // node.getAttributes().getNamedItem("ID").getNodeValue();

    // Get the value of all sub-elements.
    NodeList nameNodes = elem.getElementsByTagName("Name");
    for(int j = 0; j < nameNodes.getLength(); j++) {
        Node nameNode = nameNodes.item(j);
        staffList.add(new staff(nameNode.getTextContent()));
    }

    NodeList speedNodes = elem.getElementsByTagName("Speed");
    for(int j = 0; j < speedNodes.getLength(); j++) {
        Node speedNode = speedNodes.item(j);
        connectList.add(new connect(Integer.parseInt(speedNode.getTextContent())));
    }
}

      

PS: Try using class names starting with uppercase.

+2


source


You want getTextContent (), not getNodeValue () - the latter always returns null for element nodes. See: DOMDocument getNodeValue () returns null (contains string with escaped output)



0


source







All Articles