1

我在用 Java 解析 XML 文件时遇到了一些麻烦。该文件采用以下形式:

<root>
  <thing>
    <name>Thing1</name>
    <property>
      <name>Property1</name>
    </property>
    ...
  </thing>
  ...
</root>

最终,我想将此文件转换为一个 Thing 对象列表,其中包含一个字符串名称 (Thing1) 和一个 Property 对象列表,每个对象也都有一个名称 (Property1)。

我一直在尝试使用 xpaths 来获取这些数据,但是当我尝试获取“事物”的名称时,它会给出出现在“事物”中的所有名称,包括“属性”的名称。我的代码是:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document dom = db.parse(filename);
XPath xpath = XPathFactory.newInstance().newXPath();


XPathExpression thingExpr = xpath.compile("//thing");
NodeList things = (NodeList)thingExpr.evaluate(dom, XPathConstants.NODESET);
for(int count = 0; count < things.getLength(); count++)
{
    Element thing = (Element)things.item(count);
    XPathExpression nameExpr = xpath.compile(".//name/text()");
    NodeList name = (NodeList) nameExpr.evaluate(thing, XPathConstants.NODESET);
    for(int i = 0; i < name.getLength(); i++)
    {
        System.out.println(name.item(i).getNodeValue());    
    }
}

任何人都可以帮忙吗?提前致谢!

4

3 回答 3

1

你可以尝试类似...

public class TestXPath {

    public static void main(String[] args) {
        String xml =
                        "<root>\n"
                        + "    <thing>\n"
                        + "        <name>Thing1</name>\n"
                        + "        <property>\n"
                        + "            <name>Property1</name>\n"
                        + "        </property>\n"
                        + "        <property>\n"
                        + "            <name>Property2</name>\n"
                        + "        </property>\n"
                        + "        <property>\n"
                        + "            <name>Property3</name>\n"
                        + "        </property>\n"
                        + "        <property>\n"
                        + "            <name>Property4</name>\n"
                        + "        </property>\n"
                        + "        <property>\n"
                        + "            <name>Property5</name>\n"
                        + "        </property>\n"
                        + "    </thing>/n"
                        + "    <NoAThin>\n"
                        + "        <name>Thing2</name>\n"
                        + "        <property>\n"
                        + "            <name>Property1</name>\n"
                        + "        </property>\n"
                        + "        <property>\n"
                        + "            <name>Property2</name>\n"
                        + "        </property>\n"
                        + "        <property>\n"
                        + "            <name>Property3</name>\n"
                        + "        </property>\n"
                        + "        <property>\n"
                        + "            <name>Property4</name>\n"
                        + "        </property>\n"
                        + "        <property>\n"
                        + "            <name>Property5</name>\n"
                        + "        </property>\n"
                        + "    </NoAThin>/n"
                        + "</root>";

        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes());
            Document dom = db.parse(bais);
            XPath xpath = XPathFactory.newInstance().newXPath();

            // Find the "thing" node...
            XPathExpression thingExpr = xpath.compile("/root/thing");
            NodeList things = (NodeList) thingExpr.evaluate(dom, XPathConstants.NODESET);

            System.out.println("Found " + things.getLength() + " thing nodes...");

            // Find the property nodes of thing
            XPathExpression expr = xpath.compile("property");
            NodeList nodes = (NodeList) expr.evaluate(things.item(0), XPathConstants.NODESET);

            System.out.println("Found " + nodes.getLength() + " thing/property nodes...");

            // Find all the property "name" nodes under thing
            expr = xpath.compile("property/name");
            nodes = (NodeList) expr.evaluate(things.item(0), XPathConstants.NODESET);

            System.out.println("Found " + nodes.getLength() + " name nodes...");
            System.out.println("Property value = " + nodes.item(0).getTextContent());

            // Find all nodes that have property nodes
            XPathExpression exprAll = xpath.compile("/root/*/property");
            NodeList nodesAll = (NodeList) exprAll.evaluate(dom, XPathConstants.NODESET);
            System.out.println("Found " + nodesAll.getLength() + " property nodes...");

        } catch (Exception exp) {
            exp.printStackTrace();
        }
    }
}

这将为您提供类似的输出

Found 1 thing nodes...
Found 5 thing/property nodes...
Found 5 name nodes...
Property value = Property1
Found 10 property nodes...
于 2012-10-19T05:26:36.943 回答
0

怎么样"//thing/name/text()"

您现在使用的双斜杠name表示“树中的任何位置,不一定是直接子节点”。

于 2012-10-19T01:54:55.450 回答
0

使用这些 XPath 表达式

//thing[name='Thing1']

thing这将选择XML 文档中具有name其字符串值为 的子项的任何元素"Thing1"

也使用

//property[name='Property1']

property这将选择XML 文档中具有name其字符串值为 的子项的任何元素"Property1"

更新

要获取所有文本节点,每个节点都包含一个thing元素的字符串值,只需执行以下操作:

//thing/text()

在 XPath 2.0 中,可以自己获取字符串序列,使用:

//thing/string(.)

这对于单个 XPath 表达式是不可能的,但是可以thing像这样获取特定(第 n 个)元素的字符串值:

string((//thing)[$n])

where$n必须替换为从 1 到 的特定数字count(//thing)

因此,在您的编程语言中,您可以首先cnt通过评估此 XPath 表达式来确定:

count(//thing)

$n然后在 for from 1的循环中cnt动态生成 xpath 表达式并对其进行评估:

string((//thing)[$n])

获取property元素的所有值也是如此。

于 2012-10-19T01:56:08.340 回答