简体   繁体   English

从Java生成XML

[英]Generate XML from Java

How would I generate the following XML code by using Java and the Soap package? 如何使用Java和Soap包生成以下XML代码?

<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. 我已经看过一些教程,但是仍然对如何创建上述XML文件来调用SOAP消息感到困惑。 For example, here is some java code: 例如,以下是一些Java代码:

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

This creates this basic XML file: 这将创建以下基本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>

I recommend to use the wsimport tool and use JAXB to generate the XML content. 我建议使用wsimport工具并使用JAXB生成XML内容。

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

Please note that there is also a Maven plugin for wsimport . 请注意,还有一个wsimportMaven插件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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