Java - 将 Web 服务响应 (StreamResult) 转换为 XML 以检索子值
Java - convert web service response (StreamResult) to XML in order to retrieve child value
我的代码是这样完成的:
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>
所以这完全有效 OK.Now 如果我只想拥有 ResponseText 或 ResponseCode 值并将它们放在某些字段中我如何处理这个结果到 XML 以及如何获得那个 XML元素?
我也看了这个问题 Convert StreamResult to string or xml 但我没有看到与我的问题有什么关系,因为我已经在我的 class 代码中找到了答案,但我无法处理结果(到将其设置为 XML 我可以另外处理)
如果我只添加
StringWriter writer = new StringWriter();
String resultString2=writer.toString();
我没有引用已经有输出的 RESULT 变量。
我也试过这个:
result.getOutputStream().toString();
但结果是
java.io.PrintStream@3b9a45b3
result.toString();
也没有给我想要的结果
如何将此输出输出到 XML 元素,我可以从中检索并获取特定的 XML 元素?
提前谢谢你,
为了获得所需的元素,例如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);
}
我的代码是这样完成的:
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>
所以这完全有效 OK.Now 如果我只想拥有 ResponseText 或 ResponseCode 值并将它们放在某些字段中我如何处理这个结果到 XML 以及如何获得那个 XML元素?
我也看了这个问题 Convert StreamResult to string or xml 但我没有看到与我的问题有什么关系,因为我已经在我的 class 代码中找到了答案,但我无法处理结果(到将其设置为 XML 我可以另外处理) 如果我只添加
StringWriter writer = new StringWriter();
String resultString2=writer.toString();
我没有引用已经有输出的 RESULT 变量。
我也试过这个:
result.getOutputStream().toString();
但结果是
java.io.PrintStream@3b9a45b3
result.toString();
也没有给我想要的结果
如何将此输出输出到 XML 元素,我可以从中检索并获取特定的 XML 元素?
提前谢谢你,
为了获得所需的元素,例如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);
}