解析给定名称标记的XML childNodes?

时间:2016-07-10 15:44:08

标签: 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="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

1 个答案:

答案 0 :(得分:1)

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