Booji Boy
Booji Boy

Reputation: 25

Cannot get first child of Xml Node

My problem is in getting the first child of an XML node using the getFirstChild(). My xml is very basic, as follows :

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <network name="beep">
    <layers number="3">
        <layer index="0" lenght="3">
         ...
        </layer>
        <layer index="1" lenght="3">
         ...           
        </layer>
         ....
    </layers>
    </network>  

Java-code

import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.StringReader;
import org.xml.sax.InputSource;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

try {
DocumentBuilderFactory DBF = DocumentBuilderFactory.newInstance();
DocumentBuilder DB = DBF.newDocumentBuilder();
Document doc = DB.parse(new InputSource( new StringReader(Xml)));
doc.getDocumentElement().normalize();
Element root = doc.getDocumentElement();
NodeList Nodes =root.getElementsByTagName("network");
Node Layers = Nodes.item(0).getFirstChild();

}
catch (Exception ex)
{

}

as you can see there is an element which is a child of "network" and it is a "layer". I can successfully access to the network, getting the list of nodes, which is basically one node, but as soon as I try to get the first child of the first (and only) node with :

Node Layers = Nodes.item(0).getFirstChild();

I get an exception, and, even funnier, the exception is null.

Where's the problem?

Upvotes: 0

Views: 530

Answers (2)

Ghanshyam
Ghanshyam

Reputation: 182

Please try below code(both Files):

       1) XML File:
        _________

        <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
        <network name="beep">
          <layers number="3">
            <layer index="0" lenght="3">Hare</layer>
            <layer index="1" lenght="3">Rama</layer>
            <layer index="0" lenght="3">Hare</layer>
            <layer index="1" lenght="3">Krishna</layer>
          </layers>
        </network>
    **************************************************************
  2) Java File:
    __________

    import java.io.InputStream;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import org.w3c.dom.Document;
    import org.w3c.dom.NodeList;

    public class XMLDemo {

        public static void main(String...lsdfs) {
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            try {
              InputStream inputStream = XMLDemo.class.getClassLoader().getResourceAsStream("dataFilePackage/XmlData.xml");
              DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
              Document document = documentBuilder.parse(inputStream);
              NodeList nodeList = document.getElementsByTagName("network");
              System.out.println(nodeList.item(0).getTextContent());
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
          }
    }

Upvotes: 1

Vishwa Ratna
Vishwa Ratna

Reputation: 6390

I modified your xml to :

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<network name="beep">
  <layers number="3">
    <layer index="0" lenght="3">Vishwa</layer>
    <layer index="1" lenght="3">Ratna</layer>
  </layers>
</network>

Java code:

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class Main {

  public static void main(String[] args) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    try {
      DocumentBuilder builder = factory.newDocumentBuilder();

      InputStream inputStream = Main.class
          .getClassLoader().getResourceAsStream("resources/nodes.xml");
      Document doc = builder.parse(inputStream);
      NodeList nodes = doc.getElementsByTagName("network");
      System.out.println(nodes.item(0).getTextContent());

    } catch (FileNotFoundException | ParserConfigurationException e) {
      e.printStackTrace();
    } catch (SAXException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

O/P

Vishwa

Ratna

Upvotes: 0

Related Questions