使用 DocumentBuilder 从 xml 文件中提取值

Pulling value from an xml file using DocumentBuilder

我正在通过他们的 REST API 访问 ALM。我承认我还不是 xml 最强的,但这是我的尝试。返回给我的响应如下:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Entities TotalResults="1">
    <Entity Type="test-instance">
        <ChildrenCount>
            <Value>0</Value>
        </ChildrenCount>
        <Fields>
            <Field Name="test-id">
                <Value>13392</Value>
            </Field>
            <Field Name="os-config">
                <Value/>
            </Field>
            <Field Name="data-obj">
                <Value/>
            </Field>
            <Field Name="is-dynamic">
                <Value>N</Value>
            </Field>
            <Field Name="exec-time">
                <Value>09:41:40</Value>
            </Field>
            <Field Name="cycle">
                <Value/>
            </Field>
            <Field Name="has-linkage">
                <Value>N</Value>
            </Field>
            <Field Name="exec-event-handle">
                <Value/>
            </Field>
            <Field Name="exec-date">
                <Value>2019-06-12</Value>
            </Field>
            <Field Name="last-modified">
                <Value>2019-06-12 06:42:47</Value>
            </Field>
            <Field Name="subtype-id">
                <Value>hp.qc.test-instance.VAPI-XP-TEST</Value>
            </Field>
            <Field Name="cycle-id">
                <Value>5421</Value>
            </Field>
            <Field Name="attachment">
                <Value/>
            </Field>
            <Field Name="id">
                <Value>13404</Value>
            </Field>
            <Field Name="plan-scheduling-date"/>
            <Field Name="assign-rcyc">
                <Value/>
            </Field>
            <Field Name="test-config-id">
                <Value>14751</Value>
            </Field>
            <Field Name="owner">
                <Value>johnsmith</Value>
            </Field>
            <Field Name="pinned-baseline">
                <Value/>
            </Field>
            <Field Name="ver-stamp">
                <Value>4</Value>
            </Field>
            <Field Name="test-instance">
                <Value>1</Value>
            </Field>
            <Field Name="host-name">
                <Value/>
            </Field>
            <Field Name="order-id">
                <Value>1</Value>
            </Field>
            <Field Name="eparams">
                <Value/>
            </Field>
            <Field Name="task-status">
                <Value/>
            </Field>
            <Field Name="iterations">
                <Value/>
            </Field>
            <Field Name="environment">
                <Value/>
            </Field>
            <Field Name="actual-tester">
                <Value>johnsmith</Value>
            </Field>
            <Field Name="name">
                <Value>My Test Case</Value>
            </Field>
            <Field Name="bpta-change-awareness">
                <Value/>
            </Field>
            <Field Name="user-template-02"/>
            <Field Name="plan-scheduling-time">
                <Value/>
            </Field>
            <Field Name="user-02">
                <Value/>
            </Field>
            <Field Name="status">
                <Value>Passed</Value>
            </Field>
        </Fields>
        <RelatedEntities/>
    </Entity>
    <singleElementCollection>false</singleElementCollection>
</Entities>

我正在尝试从文件顶部附近的第一个字段 "test-id" 获取值。

我尝试使用 DocumentBuilder 提取值:

DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputSource src = new InputSource();
src.setCharacterStream(new StringReader(response.toString()));  <--response.toString() is the xml data you see above.

Document doc = builder.parse(src);
test_id = doc.getElementsByTagName("test-id").item(0).getTextContent();

不幸的是,最后一行没有任何意义。有人可以在这里给我一点推动吗?我不确定还能尝试什么。

这是使用 XPath 时的样子:

InputSource src = new InputSource();
src.setCharacterStream(new StringReader(response.toString()));

DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(src);

XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("//Field[@Name='test-id']/Value");

System.out.println(expr.evaluate(doc, XPathConstants.STRING));

XPath 表达式语法的完整解释超出了本答案的范围,但是:

  • //Field - 查找所有 Field 元素,无论它们在文档中的什么位置...
  • [@Name='test-id'] ... Name 属性的值为 test-id...
  • /Value ...其中,给我 Value 元素,它们是那些匹配 Fields
  • 的子节点

或者,您也可以非常明确地说明您要查找的节点的路径(而不是使用 //):

XPathExpression expr = xpath.compile("/Entities/Entity/Fields/Field[@Name='test-id']/Value");