简体   繁体   中英

How to unmarshal xml and map it to a POJO?

I want to unmarshal this xml document that I am receiving from a REST call:

<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>

As you can see the XML is nested, and for this I am using JAXB for unmarshalling:

 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();
    }

But, I am not getting any data set into my POJO (it's null):

@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;
}

My POJO looks like above. Also I have created Entry and Complete POJO classes. How can I fix this?

The XML has 3 namespaces:

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"

You will need to ensure that each element is in the correct namespace @XmlElement(namespace="xmlns:ns3="http://www.hello.com/IAP/im1_1_0") etc , to be able to unmarshall correctly

You may Also benefit from adding @XmlType(propOrder = { "totalEntries", ...

Have you tried just adding getters and setters to the class Hello ?

Your sample works with this simplified version of 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;
  }
}

And the Test class

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();
    }
  }

}

Prints

123
Users

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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