You need to modify your root class a little bit,
so that it will contain a Set<Wrapped>
instead of a Set<UUID>
.
@XmlRootElement(name = "root")
public class Root {
@XmlElementWrapper(name = "wrapper")
@XmlElement(name = "wrapped")
public Set<Wrapped> myWrappeds = new LinkedHashSet<>();
}
And you need a separate class for the <wrapped>
elements.
Surprisingly for me, you don't need an @XmlJavaAdapter
for id
here, because JAXB already has a built-in converter between java.util.UUID
and String
.
public class Wrapped {
@XmlAttribute(name = "id")
public UUID id;
}
I have checked the above with this XML input file
<root>
<wrapper>
<wrapped id="550e8400-e29b-11d4-a716-446655440000"/>
<wrapped id="550e8400-e29b-11d4-a716-446655440001"/>
</wrapper>
</root>
and this main
method which reproduces the original XML:
public static void main(String[] args) throws Exception {
JAXBContext context = JAXBContext.newInstance(Root.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
File file = new File("root.xml");
Root root = (Root) unmarshaller.unmarshal(file);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(root, System.out);
}