如何将XML映射到POJO

时间:2016-09-13 02:59:53

标签: java xml spring

我有以下结果:

<Result xmlns="urn:buscape" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" totalResultsAvailable="1" totalResultsReturned="1" totalPages="1" page="1" totalLooseOffers="0" xsi:schemaLocation="http://developer.buscape.com/admin/lomadee.xsd">
  <details>
    <applicationID>999999999999999</applicationID>
    <applicationVersion>1.0</applicationVersion>
    <applicationPath/>
    <date>2016-09-12T23:50:19.722-03:00</date>
    <elapsedTime>19</elapsedTime>
    <status>success</status>
    <code>0</code>
    <message>success</message>
  </details>
  <lomadeeLinks>
    <lomadeeLink>
      <id>1</id>
      <originalLink>link</originalLink>
      <redirectLink>link2</redirectLink>
      <code>0</code>
      </lomadeeLink>
  </lomadeeLinks>
</Result>

看看"2.7.4 Retrieving XML data via HTTP GET",我必须将这个XML映射到Java中类似POJO的对象,这是一个问题,我找不到@Root和@Element注释,而且我是不确定如何正确地将XML映射到Java对象。

1 个答案:

答案 0 :(得分:0)

如果您使用的是spring / springboot,那么您只需使用即可     (YourPOJO)getWebServiceTemplate()marshalSendAndReceive(yourSOAPService);

POJO: -

@XmlAccessorType(XmlAccessType.FIELD) @XmlType(name =&#34;&#34;,propOrder = {     &#34; getOrdersResponse&#34; }) @XmlRootElement(name =&#34; nameOfTag&#34;,namespace =&#34; http://example.org/yourResource&#34;) 公共类GetOrdersByDateResult {

@XmlElement(name = "GetOrdersResponse")
protected GetOrdersResponseType getOrdersResponse;

getter();
setter();

}