访问JAX-WS Dispatch响应的内容 [英] Accessing the content of a JAX-WS Dispatch Response

查看:213
本文介绍了访问JAX-WS Dispatch响应的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JAX-WS访问Web服务:

I'm trying to access a web service with JAX-WS using:

Dispatch<Source> sourceDispatch = null;
sourceDispatch = service.createDispatch(portQName, Source.class, Service.Mode.PAYLOAD);
Source result = sourceDispatch.invoke(new StreamSource(new StringReader(req)));
System.out.println(sourceToXMLString(result));

其中:

private static String sourceToXMLString(Source result) {
    String xmlResult = null;
    try {
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        //transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        OutputStream out = new ByteArrayOutputStream();
        StreamResult streamResult = new StreamResult();
        streamResult.setOutputStream(out);
        transformer.transform(result, streamResult);
        xmlResult = streamResult.getOutputStream().toString();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return xmlResult;
}

访问响应内容的正确方法是什么,例如: 获取响应中特定元素的内容

What is the proper way to access the contents of the response, eg. get the content of a specific element in the response?

所有可用的示例只打印完整的XML响应:(

All the available examples just print the full XML response :(

推荐答案

尝试将Transformer#transform()与DOMResult一起使用,然后使用生成的节点。

Try using Transformer#transform() with a DOMResult and then use the resulting Node.

private static void sourceToXML(Source result) {
    Node rootNode= null;
    try {
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        DOMResult domResult = new DOMResult();
        transformer.transform(result, domResult );
        rootNode = domResult.getNode()
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    // Process rootNode here
}

这篇关于访问JAX-WS Dispatch响应的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆