Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用xpath在Java中解析带有名称空间的XML_Java_Xml_Soap_Xpath - Fatal编程技术网

使用xpath在Java中解析带有名称空间的XML

使用xpath在Java中解析带有名称空间的XML,java,xml,soap,xpath,Java,Xml,Soap,Xpath,我试图用java解析SOAP请求,但代码并没有返回任何节点 这是任何人都能找到错误的代码 String xml="<soapenv:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" x

我试图用java解析SOAP请求,但代码并没有返回任何节点 这是任何人都能找到错误的代码

        String xml="<soapenv:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://services.web.post.list.com\"><soapenv:Header><authInfo xsi:type=\"soap:authentication\" xmlns:soap=\"http://list.com/services/SoapRequestProcessor\"><!--You may enter the following 2 items in any order--><username xsi:type=\"xsd:string\">dfasf@google.com</username><password xsi:type=\"xsd:string\">PfasdfRem91</password></authInfo></soapenv:Header></soapenv:Envelope>"; 
    System.out.println(xml);
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document doc = builder.parse(new InputSource(new StringReader(xml)));
    XPath xpath = XPathFactory.newInstance().newXPath();
    // XPath Query for showing all nodes value

    try
    {
    XPathExpression expr = xpath.compile("/soapenv:Envelope/soapenv:Header/authInfo/password");
    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nodes = (NodeList) result;
    System.out.println("Got " + nodes.getLength() + " nodes");
//    System.out.println(nodes.item(0).getNodeValue());
    }
    catch(Exception E)
    {
        System.out.println(E);
    }
stringxml=”dfasf@google.comPfasdfRem91"; 
System.out.println(xml);
DocumentBuilderFactory domFactory=DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder=domFactory.newDocumentBuilder();
documentdoc=builder.parse(新的InputSource(新的StringReader(xml));
XPath=XPathFactory.newInstance().newXPath();
//用于显示所有节点值的XPath查询
尝试
{
XPathExpression expr=xpath.compile(“/soapenv:Envelope/soapenv:Header/authInfo/password”);
Object result=expr.evaluate(doc,XPathConstants.NODESET);
节点列表节点=(节点列表)结果;
System.out.println(“Got”+节点.getLength()+节点”);
//System.out.println(nodes.item(0.getNodeValue());
}
捕获(例外E)
{
系统输出打印ln(E);
}


您需要在
XPath
上设置
NamespaceContext

演示

package forum11644994;

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

import javax.xml.namespace.NamespaceContext;
import javax.xml.parsers.*;
import javax.xml.xpath.*;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class Demo {

    public static void main(String[] args) throws Exception {
        String xml = "<soapenv:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://services.web.post.list.com\"><soapenv:Header><authInfo xsi:type=\"soap:authentication\" xmlns:soap=\"http://list.com/services/SoapRequestProcessor\"><!--You may enter the following 2 items in any order--><username xsi:type=\"xsd:string\">dfasf@google.com</username><password xsi:type=\"xsd:string\">PfasdfRem91</password></authInfo></soapenv:Header></soapenv:Envelope>";
        System.out.println(xml);
        DocumentBuilderFactory domFactory = DocumentBuilderFactory
                .newInstance();
        domFactory.setNamespaceAware(true);
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        Document doc = builder.parse(new InputSource(new StringReader(xml)));
        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/";
                }
                return null;
            }
        });
        // XPath Query for showing all nodes value

        try {
            XPathExpression expr = xpath
                    .compile("/soapenv:Envelope/soapenv:Header/authInfo/password");
            Object result = expr.evaluate(doc, XPathConstants.NODESET);
            NodeList nodes = (NodeList) result;
            System.out.println("Got " + nodes.getLength() + " nodes");
            // System.out.println(nodes.item(0).getNodeValue());
        } catch (Exception E) {
            System.out.println(E);
        }

    }
}
用于UM11644994的包;
导入java.io.StringReader;
导入java.util.Iterator;
导入javax.xml.namespace.NamespaceContext;
导入javax.xml.parsers.*;
导入javax.xml.xpath.*;
导入org.w3c.dom.Document;
导入org.w3c.dom.NodeList;
导入org.xml.sax.InputSource;
公开课演示{
公共静态void main(字符串[]args)引发异常{
字符串xml=”dfasf@google.comPfasdfRem91";
System.out.println(xml);
DocumentBuilderFactory domFactory=DocumentBuilderFactory
.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder=domFactory.newDocumentBuilder();
documentdoc=builder.parse(新的InputSource(新的StringReader(xml));
XPath=XPathFactory.newInstance().newXPath();
setNamespaceContext(新的NamespaceContext(){
@凌驾
公共迭代器getPrefixes(字符串arg0){
返回null;
}
@凌驾
公共字符串getPrefix(字符串arg0){
返回null;
}
@凌驾
公共字符串getNamespaceURI(字符串arg0){
if(“soapenv”.equals(arg0)){
返回“http://schemas.xmlsoap.org/soap/envelope/";
}
返回null;
}
});
//用于显示所有节点值的XPath查询
试一试{
XPathExpression expr=xpath
.compile(“/soapenv:Envelope/soapenv:Header/authInfo/password”);
Object result=expr.evaluate(doc,XPathConstants.NODESET);
节点列表节点=(节点列表)结果;
System.out.println(“Got”+节点.getLength()+节点”);
//System.out.println(nodes.item(0.getNodeValue());
}捕获(例外E){
系统输出打印ln(E);
}
}
}
输出

<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://services.web.post.list.com"><soapenv:Header><authInfo xsi:type="soap:authentication" xmlns:soap="http://list.com/services/SoapRequestProcessor"><!--You may enter the following 2 items in any order--><username xsi:type="xsd:string">dfasf@google.com</username><password xsi:type="xsd:string">PfasdfRem91</password></authInfo></soapenv:Header></soapenv:Envelope>
Got 1 nodes
dfasf@google.comPfasdfRem91
有1个节点

在最新回复中添加更多内容。。 要获得特定的节点值,可以使用下面的-- [
System.out.println(nodes.item(0.getTextContent());
]

尝试以下方法: