Forums Register Login

Can JAXB unmarshals child elements

+Pie Number of slices to send: Send
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.

Can you smell this for me? I think this tiny ad smells like blueberry pie!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1912 times.
Similar Threads
What type of marshalling does JAXB use?
java and xml parser
Help needed with unmarsheller
how to get XML form javaobject using JAXB
Question JAXB 2.0 ValidationEventHandler
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 16:13:58.