英文:
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 :
<?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?
答案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:
_________
<?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());
}
}
}
答案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 :
<?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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论