Reputation: 2519
f.e I want to have an xml where values of my collection is stored in alphabetical order:
<tag1>a</tag1>
<tag1>b</tag1>
<tag1>c</tag1>
is it possible with jaxb?
Thank you.
Upvotes: 2
Views: 1053
Reputation:
Use an ordered collection on the Java side of things.
Root
The tag1
property is a SortedSet
. You could pass in a Comparator
when you create the TreeSet
:
package forum9096805;
import java.util.*;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Root {
private SortedSet<String> tag1 = new TreeSet<String>();
public SortedSet<String> getTag1() {
return tag1;
}
public void setTag1(SortedSet<String> tag1) {
this.tag1 = tag1;
}
}
Demo
package forum9096805;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Root.class);
Root root = new Root();
root.getTag1().add("b");
root.getTag1().add("c");
root.getTag1().add("a");
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(root, System.out);
}
}
Output
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
<tag1>a</tag1>
<tag1>b</tag1>
<tag1>c</tag1>
</root>
Upvotes: 3
Reputation: 80176
Question should be: Is it possible in XSD (XML Schmea)? And the answer is NO. But you can make sure the XML instance is containing the data in alphabetical order by using ordered collection like List implementations or an array.
Upvotes: 2