从Java对象生成SOAP请求

从Java对象生成SOAP请求,soap,jaxb,Soap,Jaxb,我需要从Java中的请求对象创建Soap请求。需要的是: <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/"> <SOAP-ENV:Header/> <SOAP-ENV:Body> <tem:tag1> <tem:tag2>

我需要从Java中的请求对象创建Soap请求。需要的是:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
    <SOAP-ENV:Header/>
    <SOAP-ENV:Body>
        <tem:tag1>
        <tem:tag2>
        <MyDataSet>
            <!-- more elements within-->
        </MyDataSet>
        <tem:tag1>
        <tem:tag2>
    </SOAP-ENV:Body>
    <atom/>
</SOAP-ENV:Envelope>

假设web服务已经发布了WSDL,我建议您不用滚动自己的客户机代码,而是使用wsimport生成它,wsimport是javajdk中包含的一个实用工具。可以找到一篇关于如何使用它的文章

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
    <SOAP-ENV:Header/>
    <SOAP-ENV:Body>
        <MyDataSet>
            <!-- more elements within-->
        </MyDataSet>
    </SOAP-ENV:Body>
    <atom/>
</SOAP-ENV:Envelope>
 public static void main(String[] args) throws Exception
    {
        MyRequest request = new MyRequest();
        Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
        Marshaller marshaller = JAXBContext.newInstance(MyRequest.class).createMarshaller();
        marshaller.marshal(request, document);
        SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
        soapMessage.getSOAPPart().getEnvelope().addNamespaceDeclaration("tem", "http://tempuri.org/");
        soapMessage.getSOAPBody().addDocument(document);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        soapMessage.writeTo(outputStream);
        String output = new String(outputStream.toByteArray());
        System.out.println(output);
    }