protected Document parse(String url) throws Exception {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(url);
println("Parsed to DOM4J tree using SAX: " + document);
// now lets make a DOM object
DOMWriter domWriter = new DOMWriter();
org.w3c.dom.Document domDocument = domWriter.write(document);
println("Converted to DOM tree: " + domDocument);
// now lets read it back as a DOM4J object
DOMReader domReader = new DOMReader();
document = domReader.read(domDocument);
println("Converted to DOM4J tree using DOM: " + document);
// now lets write it back as SAX events to
// a SAX ContentHandler which should build up a new document
SAXContentHandler contentHandler = new SAXContentHandler();
SAXWriter saxWriter = new SAXWriter(contentHandler, null,
contentHandler);
saxWriter.write(document);
document = contentHandler.getDocument();
println("Converted DOM4J to SAX events then back to DOM4J: " + document);
return document;
}
|