0

我的代码完成如下:

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        Source sourceContent = soapResponse.getSOAPPart().getContent();
        System.out.print("\nResponse SOAP Message = ");
        StreamResult result = new StreamResult(System.out);
        transformer.transform(sourceContent, result);

并且它在控制台中有这个输出(结果在一个输出行中,但我会像漂亮的 xml 一样复制粘贴它只是为了更清楚)。

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <VerifyEmailResponse xmlns="http://ws.cdyne.com/">
      <VerifyEmailResult>
        <ResponseText>Mail Server will accept email</ResponseText>
        <ResponseCode>3</ResponseCode>
        <LastMailServer>gmail-smtp-in.l.google.com</LastMailServer>
        <GoodEmail>true</GoodEmail>
      </VerifyEmailResult>
    </VerifyEmailResponse>
  </soap:Body>
</soap:Envelope>

所以这完全可以正常工作。现在,如果我只想拥有 ResponseText 或 ResponseCode 值并将它们放在某些字段中,我如何将此结果处理为 XML 以及如何获取该 XML 元素?

我也看了这个问题Convert StreamResult to string or xml 但我看不到与我的问题的关系,因为我已经在 ANSWER 的类代码中,但我无法处理 RESULT(将它作为我可以处理的 XML另外)如果我只添加

StringWriter writer = new StringWriter();
String resultString2=writer.toString();

我没有引用已经有输出的 RESULT 变量。

我也试过这个:

result.getOutputStream().toString();

但结果是

java.io.PrintStream@3b9a45b3

result.toString();也没有给我想要的结果

如何将此输出获取到我可以从中检索和获取特定 XML 元素的 XML 元素?

先感谢您,

4

1 回答 1

1

为了ResponseCode, ResponseText从 xml 的输出字符串中获取所需的元素,您可以使用以下内容:

因为您的 xml 具有名称空间,所以 xpath 应该知道这一点。因此,您使用xpath.setNameSpaceContext(ns);如下方式设置命名空间上下文

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
Source sourceContent = soapResponse.getSOAPPart().getContent();
System.out.print("\nResponse SOAP Message = ");

StringWriter sw = new StringWriter();
transformer.transform( sourceContent, new StreamResult( sw ) );

InputSource inputSource = new InputSource( new StringReader( sw.toString() ) );
XPath xpath = XPathFactory.newInstance().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( "responsens".equals( prefix ) )
        {
            return "http://ws.cdyne.com/";
        }


        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);
 Object obj = xpath.evaluate("//responsens:ResponseText/text()", inputSource, XPathConstants.STRING );
 if ( obj != null ) 
 { 
       String responseText = obj.toString();
       System.out.println("Response text : " + responseText);
 }

 inputSource = new InputSource( new StringReader( sw.toString() ) );
  //To get Response code:
 obj = xpath.evaluate("//responsens:ResponseCode/text()", inputSource, XPathConstants.STRING );
 if ( obj != null ) 
 { 
    String responseCode = obj.toString();
    System.out.println("Response code : " + responseCode);
 }
于 2017-06-27T11:34:05.093 回答