Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

JAXB: Wrap property gourps

0 views
Skip to first unread message

Stanimir Stamenkov

unread,
Jun 17, 2008, 11:47:46 AM6/17/08
to
[Followup-To: comp.lang.java.programmer, but change it if you find
more appropriate group.]

I'm new to using JAXB and I'm wondering whether I could make the
following bean:

@XmlRootElement(name="doc")
public class MyBean {

public String foo;
public String bar;
public String baz;
public String taz;

}

serialize as (and deserialize from):

<doc>
<head>
<foo>...</foo>
<bar>...</bar>
</head>
<body>
<foo>...</foo>
<bar>...</bar>
</body>
</doc>

i.e. how could I annotate the bean class to achieve it?

I've seen there's an @XmlElementWrapper annotation but it is used
only with collection type properties. Here's the sample code I'm
trying with:

JAXBContext jaxbContext = JAXBContext.newInstance(MyBean.class);
Marshaller marshaller = jaxbContext.createMarshaller();

MyBean data = new MyBean();
data.foo = "...";
data.bar = "...";
data.baz = "...";
data.taz = "...";

marshaller.setProperty("jaxb.formatted.output", Boolean.TRUE);
marshaller.setProperty("jaxb.fragment", Boolean.TRUE);
marshaller.marshal(data, System.out);

--
Stanimir

0 new messages