使用DocumentBuilder从xml文件中提取值

时间:2019-06-12 14:45:16

标签: java xml alm

我正在通过其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();

不幸的是,最后一行等于零。有人可以在这里给我一点推动力吗?我不确定还有什么尝试。

1 个答案:

答案 0 :(得分:1)

这是使用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元素是与Field相匹配的那些子节点的子节点

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

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