不使用Java中的Xpath获取xml标记值

时间:2017-10-15 09:09:39

标签: java xml xpath

我有一个XML对象并且我正在转换为Doc并尝试使用Xpath获取其中一个标记,它返回空白。请找到代码。

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
    <ns:BB_SDF_Response
        xmlns:ns="<url>">
        <ns:BBServiceResponse
            xmlns="<url>">
            <ns1:BBHeader
                xmlns:ns1="<url>">
                <ns1:rsHeader>
                    <ns1:status>
                        <ns1:overallStatus>E</ns1:overallStatus>
                        <ns1:bbMessages>
                            <ns1:formattedMessage>Location does not exist</ns1:formattedMessage>
                        </ns1:bbMessages>
                    </ns1:status>
                </ns1:rsHeader>
            </ns1:BBHeader>
        </ns:BBServiceResponse>
    </ns:BB_SDF_Response>
</soapenv:Body>
</soapenv:Envelope>

我的Java代码是

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
                        dbf.setNamespaceAware(true); 
DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 
org.w3c.dom.Document doc = docBuilder.parse(new InputSource(new StringReader(soapResponse)));
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();  
logger.info(xpath.evaluate("/Envelope/Body/BB_SDF_Response/BBServiceResponse/BBHeader/rsHeader/status/bbMessages/formattedMessage/",doc, XPathConstants.STRING));

我想获取formattedMessage标记值。

0 个答案:

没有答案