Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unmarshalling of nested child elements using jaxb

 
Junliang Lim
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I ask regarding the java jaxb, if we have nested child elements in the xml document, how do we unmarshal it using java objects?

I am able to unmarshal all the single elements.
However for the nested child, I have difficulty unmarshalling the phone-type and phone-number in the PhoneNumber.class.

Attached are my files and the main method below to call the unmarshalling of xml.

<person>
<id>123</id>
<first-name>john</first-name>
<last-name>tan</last-name>
<phone-numbers total="1">
<phone-number>
<phone-type>mobile</phone-type>
<phone-number>12345678</phone-number>
</phone-number>
</phone-numbers>
<email-address>asd@gmail.com</email-address>
</person>







Thank you!
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Junliang,
Questions regarding XML are better answered in our XML forum. I am moving this to the XML forum.
 
g tsuji
Ranch Hand
Posts: 669
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[0] You've decided to let go the attribute "total", that's fine as you like it to be.

[1] This is how you can rewrite your UserLinkedIn class taking into account of the wrapping element phone-numbers. The simplest way is to make it an inner class of UserLinkedIn so that the general layout will be preserve with minimum rewrite. This is how.

[2] Now the unmarshalling to verify everything is in place can look like this.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic