【问题标题】:Parsing XML childNodes of a given name tag?解析给定名称标签的 XML 子节点?
【发布时间】:2016-11-12 14:45:00
【问题描述】:

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

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

【问题讨论】:

  • 每个节点上的路径 "//rule/*" 和 xpath ".//@*"
  • 查看我的答案,我使用了 dom 解析器,输出与您的预期输出相同。

标签: java xml parsing xpath


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-16
    • 1970-01-01
    • 2018-12-05
    • 2020-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多