使用 XPath Java 解析 SOAP 响应

Ani*_*ban 1 java xml xpath

我是 XPath 新手。我有以下 SOAP 响应:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <addParentResponse xmlns="urn:JadeWebServices/NetsuiteCustomer/">
            <addParentResult>Organisation xxxxx already exists - use UpdateParent method instead</addParentResult>
        </addParentResponse>
    </soap:Body>
</soap:Envelope>
Run Code Online (Sandbox Code Playgroud)

任何人都可以给我一些可以读取“addParentResult”值的代码吗?

问候,阿尼班。

Som*_*ude 5

以下 xpath 应给出所需的结果:

/soap:Envelope/soap:Body/parentns:addParentResponse/parentns:addParentResult/text()

我添加到 xpath 的原因parentns是您的 xml 具有名称空间,并且您的 xpath 处理器应该了解它们。但addParentResponse没有前缀并且有默认的命名空间。在这种情况下,在 xpath 表达式中添加一个前缀,并在执行此操作之前告诉 xpath 处理器对于该parentns前缀,有一个值是"urn:JadeWebServices/NetsuiteCustomer/"。它是通过一个NamespaceContext.

还要确保告诉DocumentBuilderFactory它应该通过使用来了解名称空间setNamespaceAware( true );

Java 中的代码是:

    try 
    {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder db = dbf.newDocumentBuilder();

        Document doc = db.parse( new File( "soapResponse.xml" ) );

        XPathFactory xPathFactory = XPathFactory.newInstance();
        XPath xpath = xPathFactory.newXPath();
        javax.xml.namespace.NamespaceContext ns = new javax.xml.namespace.NamespaceContext()
        {

            @Override
            public String getNamespaceURI(String prefix) 
            {
                if ( "soap".equals( prefix ) )
                {
                    return "http://schemas.xmlsoap.org/soap/envelope/";
                }
                else if ( "xsi".equals( prefix ) )
                {
                    return "http://www.w3.org/2001/XMLSchema-instance";
                }
                else if ( "xsd".equals( prefix ) )
                {
                    return "http://www.w3.org/2001/XMLSchema";
                }
                else if ( "xml".equals( prefix ) )
                {
                    return javax.xml.XMLConstants.XML_NS_URI;
                }
                else if ( "parentns".equals( prefix ) )
                {
                    return "urn:JadeWebServices/NetsuiteCustomer/";
                }

                return javax.xml.XMLConstants.NULL_NS_URI;
           }

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

            @Override
            public Iterator<?> getPrefixes(String namespaceURI) 
            {
                return null;
            }

        };


         xpath.setNamespaceContext(ns);
         XPathExpression expr = xpath.compile( "/soap:Envelope/soap:Body/parentns:addParentResponse/parentns:addParentResult/text()" );


         Object exprEval = expr.evaluate( doc, XPathConstants.STRING );
         if ( exprEval != null )
         {
            System.out.println( "The text of addParentResult is : " + exprEval );
         }
    }
    catch ( Exception e )
    {
        e.printStackTrace();
    }
 }
Run Code Online (Sandbox Code Playgroud)

要测试此代码,请将 xml 放入soapResponse.xml与 java 文件处于同一级别的文件中。

输出为System.out.println():

The text of addParentResult is : Organisation xxxxx already exists - use UpdateParent method instead