Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Can JAXB unmarshals child elements  RSS feed

 
Amruth Puppala
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am able to get the root element with the follwoing approach with uses JAXB with Stax parser.
But It is working for getting the rool element of XML.but if we want to get the child element it is returning Object with null values.

Example
<foo>
<xyz>
<abc></abc>
</xyz>
</foo>

SO now I can create foo obect correctly , but I am not able to create xyz or abc objects.

For root I did like this.

JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" ); //created context to work with root node
Unmarshaller unmarshaller= jc.createUnmarshaller();
javax.xml.stream.XMLStreamReader xmlStreamReader =
javax.xml.stream.XMLInputFactory().newInstance().createXMLStreamReader( ... );
JAXBElement root = unmarshaller.unmarshal(xmlStreamReader,com.acme.foo.class);
com.acme.Foo foo= root.getValue()


for abc I did like this

JAXBContext jc = JAXBContext.newInstance( "com.acme.abc" ); //created context to work with child node
Unmarshaller unmarshaller= jc.createUnmarshaller();
javax.xml.stream.XMLStreamReader xmlStreamReader =
javax.xml.stream.XMLInputFactory().newInstance().createXMLStreamReader( ... );
JAXBElement root = unmarshaller.unmarshal(xmlStreamReader,com.acme.abc.class);
com.acme.abc foo= root.getValue()


This code is not working.

Can some once help in getting the child elemntby calling unmarshalling.

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!