【问题标题】:how to convert string (XML) to soap message如何将字符串(XML)转换为肥皂消息
【发布时间】:2018-07-03 18:43:54
【问题描述】:

当我使用下面的 SOAPBOAY 代码时,我得到了 null,这是怎么回事。过去3天我很挣扎。在我的项目中,我想将响应转换为类对象,但是当我打印 message.getSOAPBody().toString() 时,它的值为 NULL。请任何答案,这将有助于我的服务自动化项目。

String resMessage = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" + "<soapenv:Envelope\r\n"
        + "        xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"\r\n"
        + "        xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\r\n"
        + "        xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\r\n" + "  <soapenv:Header>\r\n"
        + "    <ns1:RequestHeader\r\n"
        + "         soapenv:actor=\"http://schemas.xmlsoap.org/soap/actor/next\"\r\n"
        + "         soapenv:mustUnderstand=\"0\"\r\n"
        + "         xmlns:ns1=\"https://www.google.com/apis/ads/publisher/v201705\">\r\n"
        + "      <ns1:networkCode>123456</ns1:networkCode>\r\n"
        + "      <ns1:applicationName>DfpApi-Java-2.1.0-dfp_test</ns1:applicationName>\r\n"
        + "    </ns1:RequestHeader>\r\n" + "  </soapenv:Header>\r\n" + "  <soapenv:Body>\r\n"
        + "    <getAdUnitsByStatement xmlns=\"https://www.google.com/apis/ads/publisher/v201705\">\r\n"
        + "      <filterStatement>\r\n" + "        <query>WHERE parentId IS NULL LIMIT 500</query>\r\n"
        + "      </filterStatement>\r\n" + "    </getAdUnitsByStatement>\r\n" + "  </soapenv:Body>\r\n"
        + "</soapenv:Envelope>";

// Create SoapMessage
MessageFactory msgFactory = MessageFactory.newInstance();
SOAPMessage message = msgFactory.createMessage();
SOAPPart soapPart = message.getSOAPPart();

// Load the SOAP text into a stream source
byte[] buffer = resMessage.getBytes();
ByteArrayInputStream stream = new ByteArrayInputStream(buffer);
StreamSource source = new StreamSource(stream);

// Set contents of message
soapPart.setContent(source);

// -- DONE

message.writeTo(System.out);

if (message != null) {
    System.out.println("The Response Body is " + message.getSOAPBody().toString());
}

【问题讨论】:

    标签: java xml soap


    【解决方案1】:

    看看这个:

    XML Document to String?

    我用这段代码解决了我的问题:

    public static String toString(Document doc) {
      try {
        StringWriter sw = new StringWriter();
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    
        transformer.transform(new DOMSource(doc), new StreamResult(sw));
        return sw.toString();
      } catch (Exception ex) {
        throw new RuntimeException("Error converting to String", ex);
      }
    }
    

    似乎有点啰嗦,但效果很好,我不需要设置输出属性(以防万一)。它甚至在输出中添加了一个 xml 声明。如果您不想要它,应该可以轻松删除它。

    【讨论】:

    • 将链接中的相关内容添加到帖子正文中。
    • @grantmiller - 给你。有点延迟,才收到通知。整个回答问题的新手。
    猜你喜欢
    • 2013-01-27
    • 2020-02-05
    • 1970-01-01
    • 1970-01-01
    • 2020-08-01
    • 1970-01-01
    • 2017-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多