1

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.

Community
  • 1
  • 1
maaartinus
  • 44,714
  • 32
  • 161
  • 320
  • I would suggest you first checkout my answer to http://stackoverflow.com/questions/6446904/how-to-canonicalize-wsdl-files-in-java – Thorbjørn Ravn Andersen Jun 23 '11 at 11:52
  • Maybe you should iterate and create a new Document. – Dimitri Jun 23 '11 at 11:53
  • 2
    **Obviously, this is no duplicate, just a related question.** This question was about the sorting, the other was about how the canonical form of WSDL looks like (and if there's a tool doing it). – maaartinus Jun 28 '11 at 08:25

2 Answers2

3

I'd do it with dom4j. A dom4j Element provides a live List view of its sub-elements, which you can sort. And with a Visitor you can modify an entire document:

Document document = DocumentHelper.parseText(someXml);
final Comparator<Element> comparator = new Comparator<Element>() {
    public int compare(Element o1, Element o2) {
        return o1.getName().compareTo(o2.getName());
    }
};
Visitor visitor = new VisitorSupport() {
    @Override
    public void visit(Element node) {
        @SuppressWarnings("unchecked") // dom4j doesn't know generics yet
        List<Element> elements = node.elements();
        Collections.sort(elements, comparator);
        super.visit(node);
    }
};
document.accept(visitor);
// now write the document back to a file

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 like Collections.sort(), so you will have to use the List returned by Element.content(). Unfortunately this means that your comparator will have to deal with Node, rather than Element, which will get very messy if you have mixed content.

ItamarG3
  • 4,092
  • 6
  • 31
  • 44
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • This looks nice, just the comparator needs to access the attribute "name" instead of the element's name. – maaartinus Jun 28 '11 at 08:27
  • If the sort criteria can be expressed as an XPath on each of the list item, dom4j's `XPath` class has a `sort(List list)` method that can do the job. e.g. `new DefaultXPath("./book/title/text()").sort(library.content());` (you may need to set the namespaces on the XPath before calling this method) – dlauzon Dec 17 '18 at 17:58
  • This answer is correct, just like you had mentioned in update, only works with `element.content()`. Please update the code to reflect the same. Though I understand its a very old answer. – Andrews Apr 30 '19 at 09:28
2

You've made the first step, which is to recognize that doing this kind of job in Java with DOM-like interfaces is tedious in the extreme. Now take the next step - it's much easier in XSLT.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164