访问JAX-WS Dispatch响应的内容

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

Dispatch 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; } 

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

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

尝试将Transformer#transform()与DOMResult一起使用,然后使用生成的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 }