遍历DOM树以获取(名称,值)对属性和叶节点 [英] Traversing a DOM tree to get (name,value) pairs of attributes and leaf nodes

查看:153
本文介绍了遍历DOM树以获取(名称,值)对属性和叶节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过DOM中的XML文件遍历,以检索所有的(名称,值)对:

I want to traverse through an XML file in DOM for the purpose of retrieving as (name,value) pairs all:


  1. 属性名称和值;

  2. 所有叶节点名称及其文本内容;

以下XML文件为例:

<?xml version="1.0" encoding="UTF-8"?>
<title text="title1">
    <comment id="comment1">
        <data> abcd </data>
        <data> efgh </data>
    </comment>
    <comment id="comment2">
        <data> ijkl </data>
        <data> mnop </data>
        <data> qrst </data>
    </comment>
</title>

我想要的名称值对是:

text=title1
id=comment1
data=abcd
data=efgh
id=commment2
data=ijkl
data=mnop
data=qrst


推荐答案

更容易的解决方案可能是使用XPath来提取所有名称值对,如以下示例所示。您也可以跳过DOM构造,并直接在 InputSource 上调用evaluate。 XPath表达式

An easier solution might be to use XPath to extract all name value pairs as in the following example. You could also skip the DOM construction and call evaluate directly on the InputSource. The XPath expression

//@* | //*[not(*)]

匹配所有属性和所有节点的联合, t有任何子节点。

matches the union of all attributes and all nodes that don't have any child nodes.

import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class Test {

    private static final String xml = "<title text='title1'>\n"
            + "  <comment id='comment1'>\n"
            + "    <data> abcd </data>\n"
            + "    <data> efgh </data>\n"
            + "  </comment>\n"
            + "  <comment id='comment2'>\n"
            + "    <data> ijkl </data>\n"
            + "    <data> mnop </data>\n"
            + "    <data> qrst </data>\n"
            + "  </comment>\n"
            + "</title>\n";

    public static void main(String[] args) throws Exception {
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = builder.parse(new InputSource(new StringReader(xml)));

        XPathFactory xpf = XPathFactory.newInstance();
        XPath xp = xpf.newXPath();
        NodeList nodes = (NodeList)xp.evaluate("//@* | //*[not(*)]", doc, XPathConstants.NODESET);

        System.out.println(nodes.getLength());

        for (int i=0, len=nodes.getLength(); i<len; i++) {
            Node item = nodes.item(i);
            System.out.println(item.getNodeName() + " : " + item.getTextContent());
        }
    }
}

这篇关于遍历DOM树以获取(名称,值)对属性和叶节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆