Reputation: 944
I have an XML document which is correctly validated by the DTD. Then, I add some elements which respect the rules defined by it and the new document is produced. But the elements I added are not ordered like it is defined in the DTD. Is there a way to ask jdom to automatically order the elements of the newly produced document (without doing it manually with addContent(int index, Content child)
)?
Upvotes: 2
Views: 197
Reputation: 17707
No, JDOM does not have a native sort-by-the-DTD mechanism.
When JDOM reads/parses a document the DTD validation is done by the underlying parser (xerces, whatever), and the parse result is given to JDOM.
The significance in that is that JDOM does not actually know which elements are affected by which components in the DTD. The "Business logic" in the DTD is not directly accessible, and is not directly "linked" to the actual JDOM Elements and attributes.
JDOM does not attempt to validate any content changes against any DTD, or schema - adding an element or attribute, deleting one, changing its content - those are all things which could cause an XML document to become invalid. JDOM only works in the "well-formed", not the "validated" domain of XML. Validation is left for parsing only.
Now, having said that, if you know exactly which part of the DTD impacts a particular Element (which sequence), then you can use the Element.sortChildren(...)
mechanism with an appropriate comparator
Upvotes: 1