简体   繁体   中英

Ignoring root element using JAXB

I am using Spring and it's REST template to bind XML from a webservice to a domain object using JAXB. The XML returned from the web service is as follows:

 <response>
     <user>
        <id>1</id>
        <name>bob</name>
        ...
     </user>
 </response>

I have a user class as follows:

public class User {

    private String id;

    private String name;

}

Is it possible to ignore the "response" element and specify the root element to "user"?

Thanks for any help.

XML that represents your class, has like a root <user> tag.

So:

  • or you use a java parser to extract the user subtree and after use JAXB,
  • otherwise you create another class response to mapping your webservice response.

I suggest the second choice.

For informations, when you use whatever XML-binding framework, you must remember the one-to-one relation between class fields and XML tags.

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