无法获取 XML 节点的第一个子节点。

huangapple go评论88阅读模式
英文:

Cannot get first child of Xml Node

问题

我的问题是如何使用 getFirstChild() 方法获取 XML 节点的第一个子节点。
我的 XML 很基本,如下所示:

<?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 代码

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) {

}

正如您所见,有一个元素是 "network" 的子元素,它是一个 "layer"。
我可以成功访问网络,获取节点列表,基本上是一个节点,但是一旦我尝试获取第一个(也是唯一的)节点的第一个子节点:

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

我会收到一个异常,更有趣的是,异常是 null

问题出在哪里?

英文:

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

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;
    &lt;network name=&quot;beep&quot;&gt;
    &lt;layers number=&quot;3&quot;&gt;
        &lt;layer index=&quot;0&quot; lenght=&quot;3&quot;&gt;
         ...
        &lt;/layer&gt;
        &lt;layer index=&quot;1&quot; lenght=&quot;3&quot;&gt;
         ...           
        &lt;/layer&gt;
         ....
    &lt;/layers&gt;
    &lt;/network&gt;  

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(&quot;network&quot;);
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?

答案1

得分: 1

请尝试以下代码(两个文件):

1)XML文件:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<network name="beep">
  <layers number="3">
    <layer index="0" length="3">Hare</layer>
    <layer index="1" length="3">Rama</layer>
    <layer index="0" length="3">Hare</layer>
    <layer index="1" length="3">Krishna</layer>
  </layers>
</network>

2)Java文件:

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());
    }
  }
}
英文:
Please try below code(both Files):
    
       1) XML File:
        _________

        &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;yes&quot;?&gt;
        &lt;network name=&quot;beep&quot;&gt;
          &lt;layers number=&quot;3&quot;&gt;
            &lt;layer index=&quot;0&quot; lenght=&quot;3&quot;&gt;Hare&lt;/layer&gt;
            &lt;layer index=&quot;1&quot; lenght=&quot;3&quot;&gt;Rama&lt;/layer&gt;
            &lt;layer index=&quot;0&quot; lenght=&quot;3&quot;&gt;Hare&lt;/layer&gt;
            &lt;layer index=&quot;1&quot; lenght=&quot;3&quot;&gt;Krishna&lt;/layer&gt;
          &lt;/layers&gt;
        &lt;/network&gt;
    **************************************************************
  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(&quot;dataFilePackage/XmlData.xml&quot;);
    	      DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    	      Document document = documentBuilder.parse(inputStream);
    	      NodeList nodeList = document.getElementsByTagName(&quot;network&quot;);
    	      System.out.println(nodeList.item(0).getTextContent());
    	    } catch (Exception e) {
    	    	System.out.println(e.getMessage());
    	    }
    	  }
    }

答案2

得分: 0

我将您的 XML 修改为:

<?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 代码:

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();
    }
  }
}

输出:

Vishwa

Ratna
英文:

I modified your xml to :

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;yes&quot;?&gt;
&lt;network name=&quot;beep&quot;&gt;
  &lt;layers number=&quot;3&quot;&gt;
    &lt;layer index=&quot;0&quot; lenght=&quot;3&quot;&gt;Vishwa&lt;/layer&gt;
    &lt;layer index=&quot;1&quot; lenght=&quot;3&quot;&gt;Ratna&lt;/layer&gt;
  &lt;/layers&gt;
&lt;/network&gt;

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(&quot;resources/nodes.xml&quot;);
      Document doc = builder.parse(inputStream);
      NodeList nodes = doc.getElementsByTagName(&quot;network&quot;);
      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

huangapple
  • 本文由 发表于 2020年4月6日 13:33:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/61053496.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定