users@jaxb.java.net

Re: Flattened Binding / Unmarshalling

From: Aleksei Valikov <valikov_at_gmx.net>
Date: Wed, 16 Jan 2008 21:30:49 +0100

Hi.
> I have a problem. I have got the following XML document:
> <foo name="something">
> <content>abc</content>
> <a><b>as</b></a>
> </foo>
>
> And I want to unmarshall it to this class:
> @XmlRootElement
> public class Foo
> @XmlAttribute(name = "name")
> String name;
> @XmlElement(name = "b")
> String b;
> }
>
> This is done by following code:
> JAXBContext context = JAXBContext.newInstance(Foo.class);
> Unmarshaller un = context.createUnmarshaller();
> Foo foo = (Foo) un.unmarshal(new File("./data/foo.xml"));
> System.out.println(foo.name);
> System.out.println(foo.b);
>
> When this code runs foo.name is set to "something", but b isn't set to "as"?
> What do I have to do to make this example running?

Try:

@XmlElementWrapper(name="a")
@XmlElement(name="b")
String b;

Bye.
/lexi