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

DOMResultでTransformer#transform()を使用してから、結果のノードを使用してみてください。

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 に答える