Possible Duplicate:
How to canonicalize WSDL files in Java?
I need to reorder some XML nodes using Java. Actually, I need to canonicalize a WSDL file, but some hand-made reordering should do as well.
I know, I can use org.w3c.dom.Document and iterate through all its children, etc., but it’s quite tedious (the NodeList is not even Iterable) and I wonder if there’s something usable.
I’d do it with dom4j. A dom4j
Elementprovides a live List view of its sub-elements, which you can sort. And with aVisitoryou can modify an entire document:It doesn’t get much easier than this.
Update: After trying to use my own solution much later, I realized that it doesn’t work that way. The list returned by
Element.elements()doesn’t likeCollections.sort(), so you will have to use the List returned byElement.content(). Unfortunately this means that your comparator will have to deal withNode, rather thanElement, which will get very messy if you have mixed content.