JAXB: Wrap property gourps

S

Stanimir Stamenkov

[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);
 
S

Stanimir Stamenkov

Tue, 17 Jun 2008 18:47:46 +0300, /Stanimir Stamenkov/:
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?

Looking at it a bit more I don't think it is possible using JAXB.
I'll create extra classes to represent the <head> and <body> elements.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,167
Latest member
SusanaSwan
Top