2

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

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

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

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

4

1 回答 1

1

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

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
}
于 2009-02-27T01:29:08.863 回答