簡體   English   中英

由於節點中存在冒號,因此無法使用Java中的XParser解析SOAP響應元素

[英]Unable to parse SOAP response elements using XParser in Java due to colon present in nodes

我正在嘗試通過Xparser從SOAP響應中解析一個節點,但是由於節點中存在:我無法到達特定的節點。 以下是我的XML:

<?xml version='1.0' encoding='ISO-8859-1'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
    <ns:processTransactionResponse  xmlns:ns="http://service.tpe.tbms.suntecgroup.com" 
        xmlns:ax212="http://rmi.java/xsd"
        xmlns:ax221="http://request.service.commons.tbms.suntecgroup.com/xsd"
        xmlns:ax213="http://io.java/xsd" 
        xmlns:ax222="http://utilities.service.commons.tbms.suntecgroup.com/xsd"
        mlns:ax225="http://response.service.commons.tbms.suntecgroup.com/xsd"
        xmlns:ax217="http://request.service.tpe.tbms.suntecgroup.com/xsd" 
        xmlns:ax219="http://response.service.tpe.tbms.suntecgroup.com/xsd">
                    <ns:return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                    xsi:type="ax225:ProcessResponse">
                        <ax225:abstractServiceObjects xsi:type="ax222:AbstractServiceObject">
                            <ax222:fieldId>2201</ax222:fieldId>
                            <ax222:format xsi:nil="true" />
                            <ax222:operator>=</ax222:operator>
                            <ax222:type>1</ax222:type>
                            <ax222:value>Customer ID was Active</ax222:value>

                        </ax225:abstractServiceObjects>

                    </ns:return></ns:processTransactionResponse>
            </soapenv:Body>
    </soapenv:Envelope>

和JAVA代碼:

 File reposnseFile  = new File("J:/SOAP/response.txt");
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(false);
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = (Document) db.parse(reposnseFile);
            XPath xpath = XPathFactory.newInstance().newXPath();
            xpath.setNamespaceContext(new NamespaceContext() {

                @Override
                public Iterator getPrefixes(String arg0) {
                    return null;
                }

                @Override
                public String getPrefix(String arg0) {
                    return null;
                }

                @Override
                public String getNamespaceURI(String arg0) {
                    if("soapenv".equals(arg0)) {
                        return "http://schemas.xmlsoap.org/soap/envelope/nsreturn/ax225abstractServiceObjects/ax222value";                      
                    }
                    return null;
                }
            });
            XPathExpression expr = xpath.compile("//soapenv:Envelope/soapenv:Body/ns:processTransactionResponse/ns:return/ax222:value");
            System.out.println((String) expr.evaluate(doc, XPathConstants.STRING));
    }

預期輸出:客戶ID為活動狀態(節點<ax222:value> )。 您的幫助將不勝感激。

第一:您的XML的xmlns:ax225錯誤

其次,因為有了名稱空間,所以可以使用它

XPath xpath = XPathFactory.newInstance().newXPath();
expression="//*[local-name()='value']/text()";
String result = xpath.compile(expression).evaluate(document);
System.out.println("RESULT="+result);

多虧了: Java中帶有名稱空間的XPath

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM