Java 解析给定名称标记的XML子节点?

Java 解析给定名称标记的XML子节点?,java,xml,parsing,xpath,Java,Xml,Parsing,Xpath,如何按照给定标记名的各个子节点出现的顺序解析它们?例如,在下面的示例中,我想按照每个“规则”标记子项出现的顺序来分析它们: <?xml version="1.0" encoding="UTF-8"?> <rules> <rule> <if name="Uniformity of Cell Size" operator="lessOrEqual" value="2.5"/> <if name="Bar

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

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

A-演示代码:DOMParserExample.java
您可以在每个节点上输入路径
“//rule/*”
,然后输入xpath
“//*”
。请选择我的答案,我使用了dom解析器,输出与预期的输出相同。
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
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);
    }

}
<?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