如何解组xml并将其映射到POJO?

时间:2017-08-22 09:29:55

标签: java xml jaxb unmarshalling

我想解组我从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类。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

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", ...

中受益

答案 1 :(得分:0)

您是否尝试过将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
相关问题