使用XPath解析单个值的xml将返回空字符串

时间:2015-12-20 09:33:06

标签: java android xpath

我正在尝试在此XML中获取此值B006PF20EI

<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>

我正在使用像这样的XPath

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;
    }
}

但看起来该方法只是返回一个emptry字符串

1 个答案:

答案 0 :(得分:1)

使用好路径:

/ItemSearchResponse/Items/Item/ASIN