Search code examples
javaxpathjdom

Retrieve element with Jdom / XPath and "


I'm working on an application that has these kinds of xml file (document.xml):

<root>
    <subRoot myAttribute="CN=Ok">
        Ok
    </subRoot>
    <subRoot myAttribute="CN=&quot;Problem&quot;">
        Problem
    </subRoot>    
</root>

I need to retrieve Element's using XPath expressions. I'm not able to retrieve the second element, which I need to select using the value of myAttribute. This is due to the &quot; character ...

Here is a test class. The second assertion is throwing an AssertionError because the object is null.

import static org.junit.Assert.assertNotNull;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import org.apache.commons.io.IOUtils;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;
import org.junit.Test;

public class XPathTest {

    @Test
    public void quotesXpath() throws JDOMException, IOException {
        Document document = getDocumentFromContent(getClasspathResource("document.xml"));

        String okXPath = "/root/subRoot[@myAttribute=\"CN=Ok\"]";
        assertNotNull(getElement(document, okXPath)); // Ok ...

        String problemXPath = "/root/subRoot[@myAttribute=\"CN=&quot;Problem&quot;\"]";
        assertNotNull(getElement(document, problemXPath)); // Why null ?
    }

    public String getClasspathResource(String filePath) throws IOException {
        try (InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(filePath)) {
            return IOUtils.toString(inputStream, StandardCharsets.UTF_8);
        }
    }

    public static Document getDocumentFromContent(String content) throws IOException, JDOMException {
        try (InputStream is = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8))) {
            SAXBuilder builder = new SAXBuilder();
            return builder.build(is);
        }
    }

    public Element getElement(Document document, String xpathExpression) throws JDOMException {
        XPath xpath = XPath.newInstance(xpathExpression);
        return (Element) xpath.selectSingleNode(document);
    }
}

The application is using Jdom 1.1.3

<dependency>
   <groupId>org.jdom</groupId>
   <artifactId>jdom</artifactId>
   <version>1.1.3</version>
</dependency>

How can I change my xpath expression so that the second element is returned ? Is this possible with this version of Jdom ?

Thank you for your help !


Solution

  • Try this expression:

        String problemXPath = "/root/subRoot[@myAttribute='CN=\"Problem\"']";
    

    Firstly, when the document is parsed, the entity &quot; is replaced with the " character, so that should be used directly in the XPath expression.

    Secondly, in XPath you can use either single or double quotes for string constants, which is convenient if you have strings that contain quotes.