1 9 10 package org.dom4j.samples.dom; 11 12 import org.dom4j.samples.AbstractDemo; 13 14 import org.dom4j.Document; 15 import org.dom4j.io.DOMReader; 16 import org.dom4j.io.DOMWriter; 17 import org.dom4j.io.SAXContentHandler; 18 import org.dom4j.io.SAXReader; 19 import org.dom4j.io.SAXWriter; 20 21 33 public class SAXDOMDemo extends AbstractDemo { 34 35 public static void main(String [] args) { 36 run(new SAXDOMDemo(), args); 37 } 38 39 public SAXDOMDemo() { 40 } 41 42 protected Document parse(String url) throws Exception { 43 SAXReader saxReader = new SAXReader(); 44 Document document = saxReader.read(url); 45 46 println("Parsed to DOM4J tree using SAX: " + document); 47 48 DOMWriter domWriter = new DOMWriter(); 50 org.w3c.dom.Document domDocument = domWriter.write(document); 51 52 println("Converted to DOM tree: " + domDocument); 53 54 DOMReader domReader = new DOMReader(); 56 document = domReader.read(domDocument); 57 58 println("Converted to DOM4J tree using DOM: " + document); 59 60 SAXContentHandler contentHandler = new SAXContentHandler(); 63 SAXWriter saxWriter = new SAXWriter(contentHandler, null, 64 contentHandler); 65 66 saxWriter.write(document); 67 document = contentHandler.getDocument(); 68 69 println("Converted DOM4J to SAX events then back to DOM4J: " + document); 70 71 return document; 72 } 73 } 74 75 113 | Popular Tags |