gpt4 book ai didi

java - 获取 XML 中子项的值

转载 作者:行者123 更新时间:2023-12-01 14:23:03 26 4
gpt4 key购买 nike

我有当前的 XML 文件:

<test>
<class name="something" class="some">
<field>
<name>something</name>
<location>Something.class</location>
<desc></desc>
</field>
</class>
</test>

为了解析 XML 文件,我有以下代码:

    public static void parse(){
try {
File xml = new File("testfile.xml");
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = db.parse(xml);

NodeList node = document.getChildNodes();
Element root = (Element) node.item(0);
NodeList classes = root.getElementsByTagName("class");
for(int i = 0; i < classes.getLength(); i++){
Node nodes = classes.item(i);
Element classElement = (Element) nodes;
System.out.println("Class name: " + classElement.getAttribute("name"));
System.out.println("Location: " + classElement.getAttribute("class"));
}

} catch (Exception ignored){

}
}

但我不知道如何获取其子元素的字段元素的值。我仅通过使用以下方式获取元素:

NodeList classes = root.getElementsByTagName("field");

我的问题是,如何获取元素字段中的这些值?

最佳答案

您可以调用.getChildNodes()field节点就像您对 root 所做的那样:

Node field = root.getElementsByTagName("field").item(0);
NodeList fieldChildren = field.getChildNodes();
for (int i = 0; i < fieldChildren.getLength(); i++) {
Node n = fieldChildren.item(i);
System.out.println("Node Type: " + n.getNodeName());
System.out.println("Node Value: \"" + n.getTextContent()+"\"");
}

输出(对于您的文件):

Node Type: #text
Node Value: "
"
Node Type: name
Node Value: "something"
Node Type: #text
Node Value: "
"
Node Type: location
Node Value: "Something.class"
Node Type: #text
Node Value: "
"
Node Type: desc
Node Value: ""
Node Type: #text
Node Value: "
"

#text节点是 <name> 之间的空格, <location><desc> .

编辑:

对于多个字段,请考虑以下 XML:

<test>
<class name="something" class="some">
<field>
<name>something</name>
<location>Something.class</location>
<desc/>
</field>
</class>
<class name="something2" class="some2">
<field>
<name>something2</name>
<location>Something.class2</location>
<desc>2</desc>
</field>
<field>
<name>something22</name>
<location>Something.class22</location>
<desc>22</desc>
</field>
</class>
</test>

使用代码:

NodeList fields = root.getElementsByTagName("field");
for (int fi = 0; fi < fields.getLength(); fi++) {
Node field = fields.item(fi);
NodeList fieldChildren = field.getChildNodes();
System.out.println("Processing Field #"+fi+":");
for (int i = 0; i < fieldChildren.getLength(); i++) {
Node n = fieldChildren.item(i);
System.out.println("\tNode Type: " + n.getNodeName());
System.out.println("\tNode Value: \"" + n.getTextContent()+"\"");
}
}

输出:

Processing Field #0:
Node Type: name
Node Value: "something"
Node Type: location
Node Value: "Something.class"
Node Type: desc
Node Value: ""
Processing Field #1:
Node Type: name
Node Value: "something2"
Node Type: location
Node Value: "Something.class2"
Node Type: desc
Node Value: "2"
Processing Field #2:
Node Type: name
Node Value: "something22"
Node Type: location
Node Value: "Something.class22"
Node Type: desc
Node Value: "22"

关于java - 获取 XML 中子项的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17384078/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com