java - 使用 DocumentBuilder 从 xml 文件中提取值

标签 java xml alm

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

不幸的是,最后一行毫无意义。有人可以在这里给我一点插入吗?我不知道还能尝试什么。

最佳答案

这就是使用 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'] ...具有值为 test-idName 属性。 .
  • /Value ...其中,给我 Value 元素,它们是那些匹配 Field 的子节点

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

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

关于java - 使用 DocumentBuilder 从 xml 文件中提取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56564914/

相关文章:

c# - 将两个网络摄像头流合并为一个实时流 - 以编程方式

python - cElementtree 和 ElementTree 有什么区别?

java - TextView 不显示在对话框中

tfs - 从全局列表中清除历史构建

Java泛型继承 "unchecked cast"

java - 如何使用类中的方法数组来执行类的方法?

python - 我可以用 lxml 创建这个 XML 文件吗?

java - 使用 REST API 更新 QC ALM 缺陷注释部分

java - 如何使用 ALM REST API 从 ALM 运行所有测试?

java - 获取我的局域网 IP 地址 (192.168.xxxx) (IPV4)