将 POJO 转换为 XML

Log*_*gic 6 java xml android soap pojo

我必须发出一个 SOAP POST 请求,如下所示

POST /sample/demo.asmx HTTP/1.1
Host: www.website.org
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "https://www.website.org/Method"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <Method xmlns="https://www.ourvmc.org/">
      <item1>string</item1>
      <item2>string</item2>
      <item3>string</item3>
      <item4>string</item4>
    </Method>
  </soap:Body>
</soap:Envelope>
Run Code Online (Sandbox Code Playgroud)

我已经达到

POST /sample/demo.asmx HTTP/1.1
Host: www.website.org
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "https://www.website.org/Method"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Method xmlns="http://tempuri.org/"/>
</soap:Body>
</soap:Envelope>
Run Code Online (Sandbox Code Playgroud)

我有一个名为 Method 和 getter setter 的 POJO 类,用于所有 item1、item2、item3、item4。我需要将该 POJO 转换为 xml 格式

<item1>string</item1>
  <item2>string</item2>
  <item3>string</item3>
  <item4>string</item4>
Run Code Online (Sandbox Code Playgroud)

然后发布它。任何人都可以建议怎么做吗?我已经研究过,但没有找到任何有用的解决方案。

小智 0

考虑使用 JAXB: https ://jaxb.java.net/2.2.11/docs/ch01.html#documentation

对于实际发布,请考虑 Apache CXF: http://cxf.apache.org/docs/index.html

有关使用 JAXB+CXF 从 POJO 生成 XML 的更多信息: http: //cxf.apache.org/docs/jaxb.html