dom4j conversion (from JDOM). I spent most of my work day converting most of my app's XML code from JDOM to dom4j.
Still working on a few fixes, but noticed that unlike JDOM, dom4j does not have an easy way of creating an Element object.
It must be created as a child of a root element or branch element.
I may be missing something in the documentation, but I was using the:
new Element(String name)
constructor of JDOM to create elements, and dom4j does not offer such a constructor since Element is an interface.
Update: After opening my eyes a little wider over the dom4j javadoc I came upon the org.jdom.tree package which has the default implementation of all the interfaces making up the dom4j DOM tree.
So I can use the:
new DefaultElement(String name)
constructor to do my dirty work instead. [dsuspense]
e.g. here's a way to create an XML document...
DocumentFactory factory = new DocumentFactory();
Document document = factory.createDocument();
Element root = document.addElement( "root" );
Element author1 = root.addElement( "author" )
.addAttribute( "name", "James" )
.addAttribute( "location", "UK" )
.addText( "James Strachan" );
Element author2 = root.addElement( "author" )
.addAttribute( "name", bob )
.addAttribute( "location", "US" )
.addText( "Bob McWhirter" );