我需要将一个XML文件附加到在Java7中使用SAAJ的SOAP对象

我需要将一个XML文件附加到在Java7中使用SAAJ的SOAP对象,java,xml,soap,saaj,Java,Xml,Soap,Saaj,我正在使用SAAJ和Java7来创建和发送SOAP消息。除了身体,我什么都准备好了。我需要在没有任何格式错误的情况下将XML文件的内容放入SOAP消息体中。我尝试过使用DOM文档,我尝试过用扫描仪逐行读取它并手动添加它,但都不起作用。DOM文档解决方案最终只删除了消息的标题和正文,而我的另一个解决方案则继续分别用“”覆盖“字符”。我已经尝试使用String.replaceWith()来解决这个问题,但是在发送SOAP消息时发生了变化。有人知道我如何做到这一点,而不必编写一个巨大的解析器来从xml

我正在使用SAAJ和Java7来创建和发送SOAP消息。除了身体,我什么都准备好了。我需要在没有任何格式错误的情况下将XML文件的内容放入SOAP消息体中。我尝试过使用DOM文档,我尝试过用扫描仪逐行读取它并手动添加它,但都不起作用。DOM文档解决方案最终只删除了消息的标题和正文,而我的另一个解决方案则继续分别用“”覆盖“字符”。我已经尝试使用String.replaceWith()来解决这个问题,但是在发送SOAP消息时发生了变化。有人知道我如何做到这一点,而不必编写一个巨大的解析器来从xml文件创建SAAJ对象吗?谢谢。

这个简单的测试适合我:

import org.junit.*;

import java.io.StringReader;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.InputSource;

import org.w3c.dom.Document;

import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPFactory;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPPart;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;

/**
 *
 * @author gpeche
 */
public class SAAJTest {

    @Test 
    public void testAddDocument() throws Exception {
        String xml = "<a><b><c>hello</c><d test='attrib'>foo</d></b>blablabla</a>";
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(new InputSource(new StringReader(xml)));

        SOAPMessage message = MessageFactory.newInstance().createMessage();
        SOAPBody body = message.getSOAPBody();
        body.addDocument(doc);
        message.saveChanges();

        message.writeTo(System.out);
        System.out.println();
    }
}
import org.junit.*;
导入java.io.StringReader;
导入javax.xml.parsers.DocumentBuilderFactory;
导入javax.xml.parsers.DocumentBuilder;
导入org.xml.sax.InputSource;
导入org.w3c.dom.Document;
导入javax.xml.soap.MessageFactory;
导入javax.xml.soap.SOAPFactory;
导入javax.xml.soap.SOAPMessage;
导入javax.xml.soap.SOAPEnvelope;
导入javax.xml.soap.SOAPPart;
导入javax.xml.soap.SOAPBody;
导入javax.xml.soap.SOAPBodyElement;
/**
*
*@author gpeche
*/
公共类SAAJTest{
@试验
public void testAddDocument()引发异常{
字符串xml=“HelloFoblabla”;
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
DocumentBuilder=factory.newDocumentBuilder();
documentdoc=builder.parse(新的InputSource(新的StringReader(xml));
SOAPMessage message=MessageFactory.newInstance().createMessage();
SOAPBody=message.getSOAPBody();
body.addDocument(doc);
message.saveChanges();
消息写入(系统输出);
System.out.println();
}
}
输出:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><a><b><c>hola</c><d test="attrib">foo</d></b>blablabla</a></SOAP-ENV:Body></SOAP-ENV:Envelope>
霍拉福布拉布拉布拉 您也可以使用

 SOAPMessage message = getSoapMessageFromString("<a><b><c>hello</c><d test='attrib'>foo</d></b>blablabla</a>");

 private static SOAPMessage getSoapMessageFromString(String xml)
        throws SOAPException, IOException {

    MessageFactory factory = MessageFactory.newInstance();

    // Create a new message object with default MIME headers and the data
    // from the XML string we passed in
    SOAPMessage message = factory
            .createMessage(
                    new MimeHeaders(),
                    new ByteArrayInputStream(xml.getBytes(Charset
                            .forName("UTF-8"))));
    return message;
}
SOAPMessage message=getSoapMessageFromString(“HelloFoblablabla”);
私有静态SOAPMessage getSoapMessageFromString(字符串xml)
抛出SOAPException,IOException{
MessageFactory=MessageFactory.newInstance();
//创建具有默认MIME头和数据的新消息对象
//从我们传入的XML字符串
SOAPMessage消息=工厂
.createMessage(
新的MIMHEADERS(),
新的ByteArrayInputStream(xml.getBytes(字符集
.forName(“UTF-8”);
返回消息;
}
如果您已经将xml文本解析为字符串对象,并且不关心解析或验证它