4

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.

5
  • How are you currently generating XML? Commented Sep 23, 2011 at 8:43
  • 1
    Why 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? Commented 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. Commented 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 :P Commented 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. Commented Sep 23, 2011 at 9:05

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

peshkira pointed out that you can add a sorted list of attributes to the element. element.setAttributes(List). Even simpler and works like a charm.
There's no guarantee that adding a sorted list works with every DOM implementation. The element could store the attributes in a hash map and "remove" the list order.
1

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 ...

2 Comments

I'm not aware of any too, but such a sorting could be an option for a XML outputter - they know how to indent (pretty print), why not offering to sort attributes by names?
We're using java to generate xaml files for c# wpf designer. This designer is our xml editor and it would have been nice if attributes were in order. We can't implement ordering ourselves there and we want to avoid adding another parsing/ordering step to the generation process, if possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.