Is there any java xml library that can create xml tags with attributes in a defined order? I have to create large xml files with 5 to 20 attributes per tag and I want the attributes ordered for better readability. Attributes which are always created should be at the beginning, followed by common attributes and rarely used attributes should be at the end.
-
How are you currently generating XML?Miserable Variable– Miserable Variable2011-09-23 08:43:09 +00:00Commented Sep 23, 2011 at 8:43
-
1Why not sort the list of attributes first and then just add them. I have no idea what your model looks like but if the attribute names are in a list it should be as simple as calling Collections.sort(list) and then you can use any XML lib to just add the attributes... or am I missing something out?peshkira– peshkira2011-09-23 08:46:09 +00:00Commented Sep 23, 2011 at 8:46
-
@peshkira he does not want to sort them, he wants to see them in the XML in some predetermined, probably not sorted, order.Miserable Variable– Miserable Variable2011-09-23 08:51:03 +00:00Commented Sep 23, 2011 at 8:51
-
ok so he can just write a simple Comparator that sorts them in the way he wants and pass it to the Collections.sort method. I still don't see the problem :Ppeshkira– peshkira2011-09-23 08:52:19 +00:00Commented Sep 23, 2011 at 8:52
-
@peshkira I wasn't aware that jdom has a "setAttributes(List)" Method that cares about the order. I've used the xml library in the java sdk. jdom works like a charm.Markus– Markus2011-09-23 09:05:51 +00:00Commented Sep 23, 2011 at 9:05
2 Answers
Here's an easy way to create a custom outputter based on jdom:
JDOM uses XmlOutputter to convert a DOM into text output. Attributes are printed with the method:
protected void printAttributes(Writer out, List attributes, Element parent,
NamespaceStack namespaces) throws IOException
As you see, the attributes are passed with a simple list. The (simple) idea would be to subclass this outputter and override the method:
@Override
protected void printAttributes(Writer out, List attributes, Element parent,
NamespaceStack namespaces) throws IOException {
Collections.sort(attributes, new ByNameCompartor());
super.printAttributes(out, attribute, parent, namespaces);
}
The implementation of a comparator shouldn't be that difficult.
2 Comments
I'm not aware of any such library.
The problem is that the XML information model explicitly states that the order the attributes of a tag are irrelevant. Therefore, any in-memory XML representation that goes to the effort of preserving the ordering will more memory than is necessary and/or implement the attribute set/get methods suboptimally.
My advice would be to implement the ordering of attributes in the presentation of your XML content; e.g. in your XML editor.
I should point out that my Answer answers the Question as written - about "keeping the attributes in order". This could mean:
- preserving the order of insertion, or
- keeping in attributes sorted according to some ordering rule; e.g. ascending order.
From your comment below that you really just want to output the attributes in sorted order. This is a simpler problem, and can be addressed by customizing the code that serializes a DOM. Andreas_D explains how you can do this with JDOM, and there may be other options.
The other point is that something is a bit broken if the order of attributes in the input to a tool makes a significant difference to how it behaves. Because the order of XML attributes is supposed to be non-significant ...