javaxmlalm

Pulling value from an xml file using DocumentBuilder


I'm accessing ALM via their REST APIs. I confess I'm not the strongest with xml yet but this is my attempt. The response returned to me looks like:

<?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>

I'm trying to get the value from first field, "test-id" near the top of the file.

I've tried to pull the value using 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();

Unfortunately that last line equates to nothing. Can someone give me a little push here? I'm not sure what else to try.


Solution

  • This is how it would look using 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));
    

    A full explanation of XPath expression syntax is beyond the scope of this answer, but:

    Alternatively you can also be very explicit about the path to the node you are looking for (instead of using //):

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