簡體   English   中英

JAVA和XML:com.sun.org.apache.xpath.internal.XPathException:無法將#STRING轉換為NodeList

[英]JAVA & XML : com.sun.org.apache.xpath.internal.XPathException: Can not convert #STRING to a NodeList

我正在編寫一個Java函數,該函數解析xml元素並提取給定的xpath表達式。 下面是功能:

public static Node getDataNode(Element payload, final HashMap<String, String> namespaces, String xpathStr) {
    Node node = null;
    try {
        // Create a namespace context based on the namespaces passed in.
        NamespaceContext ctx = new NamespaceContext() {
            public String getNamespaceURI(String prefix) {
                return namespaces.get(prefix);
            }

            public Iterator getPrefixes(String val) {
                return null;
            }

            public String getPrefix(String uri) {
                return null;
            }
        };
        XPathFactory xpathFact = XPathFactory.newInstance();
        XPath xpath = xpathFact.newXPath();
        xpath.setNamespaceContext(ctx);
        XPathExpression expr = xpath.compile(xpathStr);
        System.out.println("Got request to process node : " + payload.getLocalName() + " with " + xpathStr);
        System.out.println(xpathStr + " has been compiled successfully.");
        ((XMLElement) payload).print(System.out);
        node = (Node) expr.evaluate(payload, XPathConstants.NODE);
    } catch (XPathExpressionException ex) {
        ex.printStackTrace();
        return null;
        } catch (IOException io) {
            io.printStackTrace();
            return null;
        }
    return node;
}

以下是此功能部分的日志:

Got request to process node : Body with ".//soapenv:Body/pip:request"
".//soapenv:Body/pip:request" has been compiled successfully.
<soapenv:Body xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
      <pip:request xmlns:pip="http://xmlns.oracle.com/ServiceBusApplication/UserInterfaceTest/Pipeline">textContent</pip:request>
   </soapenv:Body>

我嘗試了不同的xpath表達式,例如//soapenv:Body/pip:request、.//soapenv:Body/pip:request,但仍然出現錯誤:

 com.sun.org.apache.xpath.internal.XPathException: Can not convert #STRING to a NodeList! at com.sun.org.apache.xpath.internal.objects.XObject.error(XObject.java:711) at com.sun.org.apache.xpath.internal.objects.XObject.nodeset(XObject.java:441) at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.getResultAsType(XPathExpressionImpl.java:357) at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.eval(XPathExpressionImpl.java:101) at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.evaluate(XPathExpressionImpl.java:182) 

請讓我知道代碼中有什么問題。感謝您在解決問題方面的幫助。 謝謝。

無法復制。 在Oracle JDK 1.5和Oracle JDK 9上使用以下MCVE代碼進行了測試。

由於Oracle JDK沒有XMLElement類型,因此getDataNode方法所做的更改會注釋掉((XMLElement) payload).print(System.out)語句。

測試

import java.io.StringReader;
import java.util.HashMap;
import java.util.Iterator;

import javax.xml.namespace.NamespaceContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
String xml = "<soapenv:Body xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
             "      <pip:request xmlns:pip=\"http://xmlns.oracle.com/ServiceBusApplication/UserInterfaceTest/Pipeline\">textContent</pip:request>\n" +
             "   </soapenv:Body>";
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
Document document = domBuilder.parse(new InputSource(new StringReader(xml)));

HashMap<String, String> namespaces = new HashMap<String, String>();
namespaces.put("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
namespaces.put("pip", "http://xmlns.oracle.com/ServiceBusApplication/UserInterfaceTest/Pipeline");

Node node = getDataNode(document.getDocumentElement(), namespaces, ".//soapenv:Body/pip:request");
System.out.println(node != null ? node.getTextContent() : null);

node = getDataNode(document.getDocumentElement(), namespaces, "/soapenv:Body/pip:request");
System.out.println(node != null ? node.getTextContent() : null);

node = getDataNode(document.getDocumentElement(), namespaces, ".//pip:request");
System.out.println(node != null ? node.getTextContent() : null);

輸出量

Got request to process node : Body with .//soapenv:Body/pip:request
.//soapenv:Body/pip:request has been compiled successfully.
null
Got request to process node : Body with /soapenv:Body/pip:request
/soapenv:Body/pip:request has been compiled successfully.
textContent
Got request to process node : Body with .//pip:request
.//pip:request has been compiled successfully.
textContent

如您所見,代碼運行良好,但是.//soapenv:Body/pip:request XPath對於給定的XML不正確,因為給定的payload元素沒有<soapenv:Body>標記。

暫無
暫無

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

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