XML string parsing Document has 0 when it should have a larger value

41 views Asked by At

Hey all I am using this code below to parse through an XML string and gather all URLs that are presented inside that XML string.

String theReturnedResult = convertStreamToString(connection.getInputStream());
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;

try {
    builder = builderFactory.newDocumentBuilder();
    Document xmlDocument = builder.parse(new ByteArrayInputStream(theReturnedResult.getBytes()));

    XPath xpath = XPathFactory.newInstance().newXPath();
    String xpathExpression = "jp06:members/jp06:role-assignments/jp06:role-assignment/jp06:url";
    NodeList nodes = (NodeList) xpath.evaluate(xpathExpression, xmlDocument, XPathConstants.NODESET);
    //Document xmlDocument = builder.parse(new ByteArrayInputStream(theReturnedResult.getBytes()));

    System.out.print(nodes.getLength());

    for (int i = 0; i < nodes.getLength(); i++) {..........

The XML itself looks like this:

<?xml version="1.0" encoding="UTF-8"?><jp06:members xmlns:jp06="http://jazz.net/xmlns/prod/jazz/process/0.6/" xmlns:jp="http://jazz.net/xmlns/prod/jazz/process/1.0/" jp:total-members="68">
    <jp06:member>
        <jp06:url>https://zzzzz.zzzzz.zz:9443/qm/process/project-areas/_rutRzzzzzzzzzzzzzzzzz/members/Bob</jp06:url>
        <jp06:user-url>https://zzzzz.zzzzz.zz:9443/jts/users/Bob</jp06:user-url>
        <jp06:role-assignments-url>https://zzzzz.zzzzz.zz:9443/qm/process/project-areas/_rutRzzzzzzzzzzzzzzzzz/members/Bob/role-assignments</jp06:role-assignments-url>
        <jp06:role-assignments>
            <jp06:role-assignment>
                <jp06:url>https://zzzzz.zzzzz.zz:9443/qm/process/project-areas/_rutRzzzzzzzzzzzzzzzzz/members/Bob/role-assignments/tester</jp06:url>
                <jp06:role-url>https://zzzzz.zzzzz.zz:9443/qm/process/project-areas/_rutRzzzzzzzzzzzzzzzzz/roles/tester</jp06:role-url>
            </jp06:role-assignment>
            <jp06:role-assignment>
                <jp06:url>https://zzzzz.zzzzz.zz:9443/qm/process/project-areas/_rutRzzzzzzzzzzzzzzzzz/members/Bob/role-assignments/default</jp06:url>
                <jp06:role-url>https://zzzzz.zzzzz.zz:9443/qm/process/project-areas/_rutRzzzzzzzzzzzzzzzzz/roles/default</jp06:role-url>
            </jp06:role-assignment>
        </jp06:role-assignments>
        <jp06:role-assignments-editor-url>https://zzzzz.zzzzz.zz:9443/qm/_ajax-modules/com.ibm.team.process.manageProcessRolesAssignmentWidget?memberUrl=https://zzzzz.zzzzz.zz:9443/qm/process/project-areas/_rutRzzzzzzzzzzzzzzzzz/members/Bob&amp;rolesUrl=https://zzzzz.zzzzz.zz:9443/qm/process/project-areas/_rutRzzzzzzzzzzzzzzzzz/roles</jp06:role-assignments-editor-url>
    </jp06:member>
    <jp06:member>.......

The issue is that I am not getting anything for:

document [#document: null]

xmlDocument [#document: null]

The variable theReturnedResult has all the XML inside of it so I am not sure why Document and xmlDocument are null?

1

There are 1 answers

2
E.Wiest On

It seems your XPath returns nothing. Could you try with (remove the s from jp06:members) :

//jp06:member/jp06:role-assignments/jp06:role-assignment/jp06:url

Or shortest form :

//jp06:role-assignment/jp06:url

EDIT : Could you try to execute this locally (set the path to your.xml after builder.parse) ?

DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
builderFactory.setNamespaceAware(true);

DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document document = builder.parse("pathtoyour.xml");

XPath xpath = XPathFactory.newInstance().newXPath();
String expression = "//jp06:role-assignment/jp06:url";
NodeList nodes = (NodeList) xpath.evaluate(expression, document, XPathConstants.NODESET);

System.out.println(nodes);