gpt4 book ai didi

java - 解析给定名称标签的 XML 子节点?

转载 作者:行者123 更新时间:2023-11-30 10:42:02 24 4
gpt4 key购买 nike

如何按照给定标签名称的出现顺序解析各个子节点?例如,在下面的示例中,我想按照它们出现的顺序解析每个“规则”标签子项:

<?xml version="1.0" encoding="UTF-8"?>
<rules>
<rule>
<if name="Uniformity of Cell Size" operator="lessOrEqual" value="2.5"/>
<if name="Bare Nuclei" operator="lessOrEqual" value="5.5"/>
<then class="class" score="2" operator="equal"/>
</rule>
<rule>
<if name="Uniformity of Cell Size" operator="lessOrEqual" value="2.5"/>
<if name="Bare Nuclei" operator="greaterThan" value="5.5"/>
<then class="class" score="4" operator="equal"/>
</rule>
</rules>

预期输出:

Uniformity of Cell Size  lessOrEqual 2.5 
Bare Nuclei lessOrEqual 5.5
class equal 2

Uniformity of Cell Size  lessOrEqual 2.5 
Bare Nuclei greaterThan 5.5
class equal 4

最佳答案

A - 演示代码:DOMParserExample.java

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class DOMParserExample {

public static void main(String[] args) {

try {
File fXmlFile = new File("sample.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);

doc.getDocumentElement().normalize();
//System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

NodeList nodeList = doc.getElementsByTagName("rule");
//System.out.println("----------------------------");

for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
NodeList children = node.getChildNodes();
for(int j = 0; j < children.getLength(); j++) {
Node child = children.item(j);
switch(child.getNodeName()) {
case "if": {
printIfNode((Element) child);
break;
}
case "then": {
printThenNode((Element) child);
break;
}
}
}
System.out.println();
}
} catch (Exception e) {
e.printStackTrace();
}
}

// specialized print method for 'if'
private static void printIfNode(Element element) {
String name = element.getAttribute("name");
String operator = element.getAttribute("operator");
String value = element.getAttribute("value");
System.out.printf("%s %s %s\n", name, operator, value);
}

// specialized print method for 'then'
private static void printThenNode(Element element) {
String cLass = element.getAttribute("class");
String operator = element.getAttribute("operator");
String score = element.getAttribute("score");
System.out.printf("%s %s %s\n", cLass, operator, score);
}

}

B - 示例文件:sample.xml

<?xml version="1.0" encoding="UTF-8"?>
<rules>
<rule>
<if name="Uniformity of Cell Size" operator="lessOrEqual" value="2.5"/>
<if name="Bare Nuclei" operator="lessOrEqual" value="5.5"/>
<then class="class" score="2" operator="equal"/>
</rule>
<rule>
<if name="Uniformity of Cell Size" operator="lessOrEqual" value="2.5"/>
<if name="Bare Nuclei" operator="greaterThan" value="5.5"/>
<then class="class" score="4" operator="equal"/>
</rule>
</rules>

C - 示例输出

Uniformity of Cell Size lessOrEqual 2.5
Bare Nuclei lessOrEqual 5.5
class equal 2

Uniformity of Cell Size lessOrEqual 2.5
Bare Nuclei greaterThan 5.5
class equal 4

关于java - 解析给定名称标签的 XML 子节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38293626/

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