Search code examples
javaxmlxpathitext7xfa

Xpath display correct results in XMLSpy but null in Java


I am trying to display all text within text nodes only, within an XFA XML document while ignoring namespaces.

I came up with an Xpath that returns the desired results within XMLSpy with xpath 1.0 but the same Xpath in Java returns null for some reason.

Xpath = //*[local-name()='text'][string-length(normalize-space(.))>0]


    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    ArrayList<String> list = new ArrayList<>();

    XPathExpression expr = xpath.compile("//*[local-name()='text'][string-length(normalize-space(.))>0]");


    NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
    for (int i = 0; i < nodes.getLength(); i++) {
        System.out.println("This prints null = " + nodes.item(i).getNodeValue());
    }

XML file wouldn't post here so it can be viewed at the link below: https://drive.google.com/file/d/1n-v3gzT-3GgxNnYKFUvMPjRQmtnkqcpY/view?usp=sharing


Solution

  • The problem is that it's not the <text> elements that contain the values, but their child text nodes.

    Replace the line

        System.out.println("This prints null = " + nodes.item(i).getNodeValue());
    

    with

        System.out.println("This does not print null = " + nodes.item(i).getFirstChild().getNodeValue());