简体   繁体   中英

Generate XML from Java

How would I generate the following XML code by using Java and the Soap package?

<soapenv:Envelope 
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:ns="http://schemas.hp.com/SM/7" 
xmlns:com="http://schemas.hp.com/SM/7/Common" 
xmlns:xm="http://www.w3.org/2005/05/xmlmime">
  <soapenv:Header/>
  <soapenv:Body>
     <ns:RetrieveString>
        <ns:model>
           <ns:keys query=Hello World;" ></ns:keys>
           <ns:instance></ns:instance>
         </ns:model>
     </ns:RetrieveString>
  </soapenv:Body>
</soapenv:Envelope>

I've gone through some tutorials, but am still confused on how to create the above XML file to call a SOAP message. For example, here is some java code:

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

This creates this basic XML file:

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

I recommend to use the wsimport tool and use JAXB to generate the XML content.

Tutorial: http://docs.oracle.com/javaee/7/tutorial/jaxws001.htm#BNAYN

Please note that there is also a Maven plugin for wsimport .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM