the_prole
the_prole

Reputation: 8985

Parsing xml for single value with XPath returns empty string

I'm trying to get this value B006PF20EI in this XML

<ItemSearchResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2011-08-01">
    <OperationRequest>...</OperationRequest>
    <Items>
        <Request>...</Request>
        <TotalResults>8</TotalResults>
        <TotalPages>1</TotalPages>
        <MoreSearchResultsUrl>...</MoreSearchResultsUrl>
        <Item>
            <ASIN>B006PF20EI</ASIN>

I'm using XPaths like so

public String parseXml(String xmlString){

    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    try {

        DocumentBuilder builder = domFactory.newDocumentBuilder();
        Document dDoc = builder.parse(new InputSource(new StringReader(xmlString)));

        XPath xPath = XPathFactory.newInstance().newXPath();
        //Node node = (Node) xPath.evaluate("/Items/Item/ASIN", dDoc, XPathConstants.NODE);
        //System.out.println(node.getNodeValue());
        String value = xPath.evaluate("/ItemSearchResponse/resp/status", dDoc);
        return value;
    } catch (Exception e) {
        Log.e("MYAPP", "exception", e);
        return null;
    }
}

But it looks like the method is just returning an emptry string

Upvotes: 0

Views: 256

Answers (1)

use the good path:

/ItemSearchResponse/Items/Item/ASIN

Upvotes: 1

Related Questions