将xml转换为Soap Request

时间:2017-05-23 18:16:27

标签: java xml soap

我想将xml作为String转换为soap请求,但是这个xml不包含信封,我试过这个

import java.io.ByteArrayInputStream;
import java.nio.charset.Charset;

import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPMessage;

public class TestSoapConverter {

    public static void main(String args[])
    {
        String message="<ISO8583PostXml>"+
                "<MsgType>0300</MsgType>"+
                "<Fields>"+
                "<field_001>080000</field_001>"+
                "<field_0090>000000</field_0090>"+
                "<field_048>0822074323</field_048>"+
                "<field_020>94</field_020>"+
                "<field_080>0822</field_080>"+
                "</Fields>"+
                "</ISO8583PostXml>";

        try{

    MessageFactory factory = MessageFactory.newInstance();
    SOAPMessage   soapMessage = factory.createMessage(new MimeHeaders(),
                new ByteArrayInputStream(message.getBytes(Charset.forName("UTF-8"))));
          System.out.println(soapMessage.getSOAPBody());

        }catch(Exception ex)
        {
            ex.printStackTrace();
        }

    }   
}

我有这个例外

  

2017年5月23日下午8:14:08   com.sun.xml.internal.messaging.saaj.soap.SOAPPartImpl lookForEnvelope   严重:SAAJ0514:无法从给定的来源创建信封,因为   根元素未命名为Envelope 2017年5月23日下午8:14:08   com.sun.xml.internal.messaging.saaj.soap.EnvelopeFactory   createEnvelope严重:SAAJ0511:无法创建给定的信封   source com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl:Unable   从给定的来源创建信封:at   com.sun.xml.internal.messaging.saaj.soap.EnvelopeFactory.createEnvelope(EnvelopeFactory.java:117)     在   com.sun.xml.internal.messaging.saaj.soap.ver1_1.SOAPPart1_1Impl.createEnvelopeFromSource(SOAPPart1_1Impl.java:69)     在   com.sun.xml.internal.messaging.saaj.soap.SOAPPartImpl.getEnvelope(SOAPPartImpl.java:128)     在   com.sun.xml.internal.messaging.saaj.soap.MessageImpl.getSOAPBody(MessageImpl.java:1351)     在   com.vericash.liquid.test.TestSoapConverter.main(TestSoapConverter.java:30)   引起:com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl:   无法从给定源创建包络,因为根元素   没有命名&#34;信封&#34;在   com.sun.xml.internal.messaging.saaj.soap.SOAPPartImpl.lookForEnvelope(SOAPPartImpl.java:154)     在   com.sun.xml.internal.messaging.saaj.soap.SOAPPartImpl.getEnvelope(SOAPPartImpl.java:121)     在   com.sun.xml.internal.messaging.saaj.soap.EnvelopeFactory.createEnvelope(EnvelopeFactory.java:110)     ......还有4个

     

原因:

     

com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl:无法执行   从给定的源创建信封,因为根元素不是   命名&#34;信封&#34;在   com.sun.xml.internal.messaging.saaj.soap.SOAPPartImpl.lookForEnvelope(SOAPPartImpl.java:154)     在   com.sun.xml.internal.messaging.saaj.soap.SOAPPartImpl.getEnvelope(SOAPPartImpl.java:121)     在   com.sun.xml.internal.messaging.saaj.soap.EnvelopeFactory.createEnvelope(EnvelopeFactory.java:110)     在   com.sun.xml.internal.messaging.saaj.soap.ver1_1.SOAPPart1_1Impl.createEnvelopeFromSource(SOAPPart1_1Impl.java:69)     在   com.sun.xml.internal.messaging.saaj.soap.SOAPPartImpl.getEnvelope(SOAPPartImpl.java:128)     在   com.sun.xml.internal.messaging.saaj.soap.MessageImpl.getSOAPBody(MessageImpl.java:1351)     在   com.vericash.liquid.test.TestSoapConverter.main(TestSoapConverter.java:30)

0 个答案:

没有答案