• Post Reply Bookmark Topic Watch Topic
  • New Topic

Unable to unmarshal an object inside another object  RSS feed

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Guys,

I have got an xml file:





AddressDto.java




CountryDto file:





When i run the code




I am able to get the street and city in AddressDto object. But the field country is displaying as null...Any idea whats going wrong here?
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, think about it for a minute. How is JAXB supposed to figure out that it needs to use an instance of CountryDto just based on the @XmlElement annotation? How does it know to make the connection between AddressDto.country and CountryDto? Computers may seem smart but they really are only good at doing what we tell them to do. In this case you told it nothing about how to make that connection so that's exactly what it did, nothing.
 
Tapan Agarwal
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Well, think about it for a minute. How is JAXB supposed to figure out that it needs to use an instance of CountryDto just based on the @XmlElement annotation? How does it know to make the connection between AddressDto.country and CountryDto? Computers may seem smart but they really are only good at doing what we tell them to do. In this case you told it nothing about how to make that connection so that's exactly what it did, nothing.



Thanks for the reply Junila....I am new to the JAXB world.What I could have done is replace the xml part



with



And then modify your addressDto as well:



This would have worked. But I cannot make any changes in the input xml schema as this service is being used by other applications as well.
Is there any way by which I could take the country and country code from the xml and map it to the corresponding fields in the CountryDto Object without making any changes in the xml?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably want to search for Marshal/unmarshal nested object to flat XML
 
Tapan Agarwal
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:You probably want to search for Marshal/unmarshal nested object to flat XML


Thanks Junilu, It worked.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!