Reputation: 2688
Is there a way to auto-increment the element names of a Collection when marshalling to an XML using JAXB? I generate an XML using this code:
public void saveTestSettings(final String filename, final DefaultTestSettings t) throws Exception
{
try
{
final Path path = FileSystems.getDefault().getPath((paths.getTestSettingsPath() + filename));
if (Files.notExists(path.getParent()))
throw new Exception(path.getParent().toString() + " does not exist!!");
final OutputStream out = new BufferedOutputStream(Files.newOutputStream(path));
final JAXBContext jaxbContext = JAXBContext.newInstance(DefaultTestSettings.class);
final Marshaller m = jaxbContext.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(t, out);
out.flush();
out.close();
}
catch (JAXBException | IOException e)
{
e.printStackTrace();
throw new Exception(e.getMessage());
}
}
The resulting XML looks like this:
<defaultTestSettings>
<groups>
<instruments>
<name>A200</name>
</instruments>
<name>A0</name>
</groups>
<groups>
<instruments>
<name>A300</name>
</instruments>
<instruments>
<name>A400</name>
</instruments>
<name>A1</name>
</groups>
</defaultTestSettings>
I would like the groups
and instruments
to be automatically incremented, so that the result looks like this:
<defaultTestSettings>
<groups1>
<instruments1>
<name>A200</name>
</instruments1>
<name>A0</name>
</groups1>
<groups2>
<instruments1>
<name>A300</name>
</instruments1>
<instruments2>
<name>A400</name>
</instruments2>
<name>A1</name>
</groups2>
</defaultTestSettings>
I am not too familiar with using XML schema, would that work?
Upvotes: 3
Views: 1671
Reputation: 149017
Below are a couple of way's this use case could be handled:
My answer below expands on the excellent answer given by Ilya. I have extended it to use an XmlAdapter
to remove the logic from the getElements()
method.
DefaultSettings
import java.util.*;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
@XmlRootElement
public class DefaultSettings {
private List<Groups> groups = new ArrayList<Groups>();
@XmlAnyElement
@XmlJavaTypeAdapter(GroupsAdapter.class)
public List<Groups> getGroups() {
return groups;
}
}
Groups
import java.util.*;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
public class Groups {
private List<Instruments> instruments = new ArrayList<Instruments>();
@XmlAnyElement
@XmlJavaTypeAdapter(InstrumentsAdapter.class)
public List<Instruments> getInstruments() {
return instruments;
}
}
Instruments
public class Instruments {
}
GroupsAdapter
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.namespace.QName;
public class GroupsAdapter extends XmlAdapter<JAXBElement<Groups>, Groups> {
private int counter = 1;
private InstrumentsAdapter instrumentsAdapter = new InstrumentsAdapter();
public InstrumentsAdapter getInstrumentsAdapter() {
return instrumentsAdapter;
}
@Override
public Groups unmarshal(JAXBElement<Groups> v) throws Exception {
return v.getValue();
}
@Override
public JAXBElement<Groups> marshal(Groups v) throws Exception {
instrumentsAdapter.resetCounter();
return new JAXBElement<Groups>(new QName("groups" + counter++), Groups.class, v);
}
}
InstrumentsAdapter
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.namespace.QName;
public class InstrumentsAdapter extends XmlAdapter<JAXBElement<Instruments>, Instruments> {
private int counter = 1;
@Override
public Instruments unmarshal(JAXBElement<Instruments> v) throws Exception {
return v.getValue();
}
@Override
public JAXBElement<Instruments> marshal(Instruments v) throws Exception {
return new JAXBElement<Instruments>(new QName("instruments" + counter++), Instruments.class, v);
}
public void resetCounter() {
counter = 1;
}
}
Demo
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(DefaultSettings.class, Groups.class, Instruments.class);
Groups groups1 = new Groups();
groups1.getInstruments().add(new Instruments());
groups1.getInstruments().add(new Instruments());
Groups groups2 = new Groups();
groups2.getInstruments().add(new Instruments());
groups2.getInstruments().add(new Instruments());
DefaultSettings ds = new DefaultSettings();
ds.getGroups().add(groups1);
ds.getGroups().add(groups2);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
GroupsAdapter groupsAdapter = new GroupsAdapter();
marshaller.setAdapter(groupsAdapter);
marshaller.setAdapter(groupsAdapter.getInstrumentsAdapter());
marshaller.marshal(ds, System.out);
}
}
Output
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<defaultSettings>
<groups1>
<instruments1/>
<instruments2/>
</groups1>
<groups2>
<instruments1/>
<instruments2/>
</groups2>
</defaultSettings>
@XmlVariableNode
ExtensionBelow is a link to how this use case can be mapped using hte @XmlVariableNode
extension that we added in EclipseLink JAXB (MOXy):
Upvotes: 3
Reputation: 29693
You can do it with @XmlAnyElement annotation.
Simple example groups
tag.
@XmlRootElement(name = "defaultTestSettings")
@XmlSeeAlso(Group.class)
public class DefaultTestSettings
{
private static int counter = 1;
private static final String PROP = "group";
List<Group> groups = new ArrayList<Group>();
@XmlAnyElement
public List<JAXBElement<Group>> getElements()
{
final List<JAXBElement<Group>> retVal = new ArrayList<>();
for (final Group g : groups)
{
retVal.add(new JAXBElement(new QName(PROP + counter++), Group.class, g));
}
return retVal;
}
}
Upvotes: 3