Reputation: 2529
f.e. I have next xml scheme:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.com"
xmlns="http://www.example.com"
elementFormDefault="qualified">
<xs:complexType name="TItem">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="id" type="xs:int" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:element name="Resource">
<xs:complexType>
<xs:sequence>
<xs:element name="item" type="TItem" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Based on that xsd jaxb generates java code using which I'm creating next xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Resource xmlns="http://www.example.com">
<item id="10">asdf</item>
<item id="2">some text</item>
<item id="9">some item</item>
<item id="14">vzxc</item>
</Resource>
So. I want to produce xml with sorted items by id. So it should be:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Resource xmlns="http://www.example.com">
<item id="2">some text</item>
<item id="9">some item</item>
<item id="10">asdf</item>
<item id="14">vzxc</item>
</Resource>
Is it possible with jaxb?
P.S. id is not unique - there could be a lot of items with id="22", etc.
Upvotes: 0
Views: 1636
Reputation: 479
Try to change your item
to a TreeSet
in Resource
@XmlRootElement(name = "Resource")
public class Resource {
@XmlElement(required = true)
protected Collection<TItem> item;
....
public Collection<TItem> getItem() {
if (item == null) {
item = new TreeSet<TItem>(cmp);//cmp is an instance of Comparator<TItem>
}
return this.item;
}
Upvotes: 2