Jono
Jono

Reputation: 18118

Xpath expression for getting an attribute value fails in Java

I am trying to get an attribute value from an XML file, but my code fails with the exception below:

11-15 16:34:42.270: DEBUG/XpathUtil(403): exception = javax.xml.xpath.XPathExpressionException: javax.xml.transform.TransformerException: Extra illegal tokens: '@', 'source'

Here is the code I use to get the node list:

private static final String XPATH_SOURCE = "array/extConsumer@source";
mDocument = XpathUtils.createXpathDocument(xml);

NodeList fullNameNodeList = XpathUtils.getNodeList(mDocument,
                XPATH_FULLNAME);

And here is my XpathUtils class:

public class XpathUtils {

    private static XPath xpath = XPathFactory.newInstance().newXPath();
    private static String TAG = "XpathUtil";

    public static Document createXpathDocument(String xml) {
        try {

            Log.d(TAG , "about to create document builder factory");
            DocumentBuilderFactory docFactory = DocumentBuilderFactory
                    .newInstance();
            Log.d(TAG , "about to create document builder ");
            DocumentBuilder builder = docFactory.newDocumentBuilder();

            Log.d(TAG , "about to create document with parsing the xml string which is: ");

            Log.d(TAG ,xml );
            Document document = builder.parse(new InputSource(
                    new StringReader(xml)));

            Log.d(TAG , "If i see this message then everythings fine ");

            return document;
        } catch (Exception e) {
            e.printStackTrace();
            Log.d(TAG , "EXCEPTION OCCURED HERE " + e.toString());
            return null;
        }
    }

    public static NodeList getNodeList(Document doc, String expr) {
        try {
            Log.d(TAG , "inside getNodeList");
            XPathExpression pathExpr = xpath.compile(expr);
            return (NodeList) pathExpr.evaluate(doc, XPathConstants.NODESET);
        } catch (Exception e) {
            e.printStackTrace();
            Log.d(TAG, "exception = " + e.toString());
        }
        return null;
    }

    // extracts the String value for the given expression
    public static String getNodeValue(Node n, String expr) {
        try {
            Log.d(TAG , "inside getNodeValue");
            XPathExpression pathExpr = xpath.compile(expr);
            return (String) pathExpr.evaluate(n, XPathConstants.STRING);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

I get an exception thrown in the getNodeList method.

Now, according to http://www.w3schools.com/xpath/xpath_syntax.asp, to get an attribute value, you use the "@" sign. But for some reason, Java is complaining about this symbol.

Upvotes: 3

Views: 14443

Answers (3)

Pops
Pops

Reputation: 30828

The w3schools page you linked to also says "Predicates are always embedded in square brackets." You just appended the @source. Try

private static final String XPATH_SOURCE = "array/extConsumer[@source]";

EDIT:
To be clear, this is if you're looking for a single item, which is what your original wording led me to believe. If you want to collect a bunch of source attributes, see the answers by vanje and Anon that suggest using a slash instead of square brackets.

Upvotes: 1

vanje
vanje

Reputation: 10383

Try

array/extConsumer/@source

as your XPath expression. This selects the source attribute of the extConsumer element.

Upvotes: 6

Anon
Anon

Reputation: 2674

Put a slash before the attribute spec:

array/extConsumer/@source

Upvotes: 1

Related Questions