I have the below class. When Marshalling, I would like to omit the tag “config”, is it possible?
@XmlRootElement(name = "config")
@XmlAccessorType(XmlAccessType.FIELD)
public static class Config {
@XmlElement(name = "dry-run")
protected Boolean dryRun;
@XmlElementWrapper(name = "filters")
@XmlElement(name = "filter")
protected List<String> filters;
public Boolean isDryRun() {
return dryRun;
}
public void setDryRun(boolean dryRun) {
this.dryRun = dryRun;
}
public List<String> getFilters() {
return filters;
}
}
Example:
Current output:
<Root>
<config xmlns:wf="nspace">
<dry-run>false</dry-run>
<filters>
<filter>
myFilter
</filter>
</filters>
</config>
</Root>
Desired output:
<Root>
<dry-run>false</dry-run>
<filters>
<filter>
myFilter
</filter>
</filters>
</Root>
UPDATE:
all I wanted to know is “can it be done ONLY with JAXB or not?”. Just check this question (not the answer), I didn’t get how he marshelled with only JAXB and no root element was written. It is precisely what I want.
So you want to marshal your object not to a single XML subtree, but to an XML fragment, i.e. a list of siblings without a parent. I believe there is no way to achieve this with Jaxb itself. But you can serialize to some intermediate form and process that. For example, you could create your own SAX
ContentHandlerand have that handler count depth and only delegate events at non-zero nesting depth.