Here's the same program using DOM instead of JDOM. Which is simpler?
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.apache.xerces.dom.*;
import org.apache.xml.serialize.*;
public class HelloDOM {
public static void main(String[] args) {
try {
DocumentBuilderFactory factory
= DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
DOMImplementation impl = builder.getDOMImplementation();
Document hello = impl.createDocument(null, "GREETING", null);
// ^^^^ ^^^^
// Namespace URI DocType
Element root = hello.getDocumentElement();
// We can't use a raw string. Instead we must first create
// a text node.
Text text = hello.createTextNode("Hello DOM!");
root.appendChild(text);
// Now that the document is created we need to *serialize* it
try {
OutputFormat format = new OutputFormat(hello);
XMLSerializer serializer
= new XMLSerializer(System.out, format);
serializer.serialize(root);
}
catch (IOException e) {
System.err.println(e);
}
}
catch (DOMException e) {
e.printStackTrace();
}
catch (ParserConfigurationException e) {
System.out.println(e);
}
}
}