users@jaxb.java.net

How to marshall values as elements?

From: Markus Karg <karg_at_quipsy.de>
Date: Thu, 5 May 2011 14:34:54 +0200

I have a possibly strange question: How can I dynamically set elements
names?

 

I need to create this:

 

<SomeOuterElement>

                <foo>val1</foo>

                <bar>val2</bar>

</SomeOuterElement>

 

Looks easy, but the actual names "foo" and "bar" are not given
statically, but must change with each instance at runtime.

 

Example:

 

@XmlRootElement public class SomeOuterElement {

    @XmlElement public List<SomeInnerElement> content;

}

 

public class SomeInnerElement {

   public name; // contains "foo" or "bar"

  public value; // contains "val1" or "val2"

}

 

Neither "foo" nor "bar" do exist as classes, those are just Strings
which have to get transported (don't ask why I want to do that -- it's
part of WebDAV's crazy spec).

 

How to achieve that?

 

Thanks!

Markus