为什么创建的XML不符合标准?

时间:2017-01-12 18:35:00

标签: java xml saaj

根据

http://docs.oracle.com/javaee/5/tutorial/doc/bnbhr.html

SOAPMessage message = messageFactory.createMessage();
SOAPHeader header = message.getSOAPHeader();
SOAPBody body = message.getSOAPBody();

以下是它生成的XML:

<SOAP-ENV:Envelope
 xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  <SOAP-ENV:Header/>
  <SOAP-ENV:Body>
    ...
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

但我的实现会生成像

这样的XML代码
<SOAP-ENV:Envelope
 xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  <SOAP-ENV:Body>
    ...
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

没有<SOAP-ENV:Header/>标记。我明确需要标头标签,即使它是空的。我怎样才能生成这样的XML。

这是我的代码

MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage message = messageFactory.createMessage();
SOAPHeader header = message.getSOAPHeader();



marshaller.marshal(someObject, message.getSOAPBody());
message.saveChanges();
return message; //save it as request. for printing in logs

我用

ByteArrayOutputStream in = new ByteArrayOutputStream();
request.writeTo(in);
logger.debug("Sending: " + in);

将其打印在我的控制台上。

1 个答案:

答案 0 :(得分:0)

好吧,现在我无法重现您所看到的内容。我使用的是SAAJ 1.3.28,我的代码如下所示。我看到即使标题中没有任何内容,我也会获得SOAP标头。你可以在下面看到我试图在标题中加入一些东西 - 我显然还没有做到这一点,因为我的评论是错误的。我以两种不同的方式打印出SOAP消息,并以两种方式显示标题。您使用的是什么版本的SAAJ?

作为参考,我的第二种邮件打印方法来自this stackoverflow post

package com.hotjoe.stuff;


import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.stream.StreamResult;

public class MessageCreator {

    public static void main(String[] argv) {
        try {
            MessageFactory factory = MessageFactory.newInstance();
            SOAPMessage message = factory.createMessage();

            SOAPBody body = message.getSOAPBody();
            QName bodyName = new QName("http://wombat.ztrade.com", "GetLastTradePrice", "m");
            SOAPBodyElement bodyElement = body.addBodyElement(bodyName);

            QName name = new QName("symbol");
            SOAPElement symbol = bodyElement.addChildElement(name);
            symbol.addTextNode("SUNW");

//            SOAPHeader header = message.getSOAPHeader();
//            QName headerName = new QName("http://wombat.ztrade.com","headerItem", "m");
//            header.addHeaderElement(headerName);

            ByteArrayOutputStream out = new ByteArrayOutputStream();
            message.writeTo(out);
            String strMsg = new String(out.toByteArray());

            System.out.println( "message is " + strMsg );

            System.out.println( "or another way " + printSoapMessage(message) );
        }
        catch( SOAPException | IOException | TransformerException se ) {
            se.printStackTrace();
        }
    }

    public static String printSoapMessage(final SOAPMessage soapMessage) throws TransformerFactoryConfigurationError,
            SOAPException, TransformerException
    {
        final TransformerFactory transformerFactory = TransformerFactory.newInstance();
        final Transformer transformer = transformerFactory.newTransformer();

        // Format it
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

        final Source soapContent = soapMessage.getSOAPPart().getContent();

        final ByteArrayOutputStream streamOut = new ByteArrayOutputStream();
        final StreamResult result = new StreamResult(streamOut);
        transformer.transform(soapContent, result);

        return streamOut.toString();
    }
}