簡體   English   中英

如何解組XML並將其映射到POJO?

[英]How to unmarshal xml and map it to a POJO?

我想解組我從REST調用中收到的xml文檔:

<ns2:hello xmlns:ns4="http://myspace.org/hello/history/1.0" xmlns:ns3="http://www.hello.com/IAP/im1_1_0" xmlns:ns2="http://www.w3.org/2005/Atom">
   <ns3:totalEntries>7</ns3:totalEntries>
   <ns2:id>123</ns2:id>
   <ns2:title type="text">Users</ns2:title>
   <ns2:updated>2017-08-22T07:51:27.270Z</ns2:updated>
   <ns2:link href="https://example.com:8080/1/rest/users" rel="self"/>
   <ns2:link href="https://example.com:8080/1/rest/users" rel="http://www.example.com/iap/im/user/create"/>
   <ns4:complete/>
   <ns2:entry>
      <ns2:id>urn:uuid:f0fd4040-04da-11e7-8f6a-8e3ecfcb7035</ns2:id>
      <ns2:title type="text">Hello</ns2:title>
      <ns2:content type="application/vnd.bosch-com.im+xml">
         <ns3:user>          
            <ns3:id>f0fd4040-04da-11e7-8f6a-8e3ecfcb7035</ns3:id>
            <ns3:name>name</ns3:name>          
            <ns3:firstName>Hello</ns3:firstName>
            <ns3:lastName>All</ns3:lastName>           
         </ns3:user>
      </ns2:content>
   </ns2:entry>  
</ns2:hello>

如您所見,XML是嵌套的,為此,我使用JAXB進行編組:

 try {
        JAXBContext jc = JAXBContext.newInstance(Feed.class);
        Unmarshaller um = jc.createUnmarshaller();
        Feed feed = (Feed) um.unmarshal(new StringReader(userEntity.getBody()));
        System.out.println(feed.getEntry().get(0).getContent().getUser().getFirstName());
    }
    catch (JAXBException e) {
        e.printStackTrace();
    }

但是,我沒有在POJO中獲取任何數據集(它為空):

@XmlRootElement(namespace="http://www.w3.org/2005/Atom", name="hello")
public class Hello {
    private String id;
    private String totalEntries;
    private String title;
    private String updated; 
    List<Entry> entry;
    Complete complete;
}

我的POJO像上面一樣。 我還創建了Entry和Complete POJO類。 我怎樣才能解決這個問題?

XML具有3個名稱空間:

xmlns:ns4="http://myspace.org/hello/history/1.0"
xmlns:ns3="http://www.hello.com/IAP/im1_1_0"
xmlns:ns2="http://www.w3.org/2005/Atom"

您將需要確保每個元素都在正確的命名空間@XmlElement(namespace="xmlns:ns3="http://www.hello.com/IAP/im1_1_0") etc ,才能正確解組

您還可以從添加@XmlType(propOrder = { "totalEntries", ...

您是否嘗試過僅將getter和setter添加到類Hello中?

您的示例使用此簡化版本的Hello。

@XmlRootElement(namespace = "http://www.w3.org/2005/Atom", name = "hello")
public class Hello {
  private String id;
  private String title;
  public String getId() {
    return id;
  }
  public void setId(String id) {
    this.id = id;
  }
  public String getTitle() {
    return title;
  }
  public void setTitle(String title) {
    this.title = title;
  }
}

和測試班

public class Test {

  public static void main(String[] args) {
    try {
      JAXBContext jc = JAXBContext.newInstance(Feed.class);
      Unmarshaller um = jc.createUnmarshaller();
      InputStream file = new FileInputStream("path/to/file/feed.xml");
      Hello feed = (Hello) um.unmarshal(file);
      System.out.println(feed.getId());
      System.out.println(feed.getTitle());
    } catch (JAXBException | FileNotFoundException e) {
      e.printStackTrace();
    }
  }

}

版畫

123
Users

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM