I need to generate an XML like this:
<Root>
<Children>
<InnerChildren>SomethingM</InnerChildren>
</Children>
</Root>
The simplest solution is creating an inner class on the Root class:
@Root
class Root{
@Element
Children element;
@Root
private static class Children{
@Element
String innerChildren;
}
}
But I want to avoid that inner class creation, since it will make things look strange while using Root objects. Is there anyway I can achieve that result without using inner classes?
Expected way to create Root objects:
Root root = new Root("Something");
What I want to avoid:
Children child = new Children("Something");
Root root = new Root(child);
// this could be achieve by injecting some annotations
// in the constructor, but it's awful
Just use a normal class instead of an inner class. It should still work:
Update:
If you do not want to create another class, you can use the
Pathannotation by specifying an XPath expression for theinnerChildrenfield. For example:Produces:
Use the
Namespaceannotation to add name spaces. For example:Produces:
If using Styles to format the XML, it’s necessary to do some modifications since they remove the
:from theElement. The result using styles is:This is what I did:
And now the result is the expected one: