1 9 10 package org.dom4j.samples; 11 12 import org.dom4j.Document; 13 import org.dom4j.Element; 14 import org.dom4j.ElementHandler; 15 import org.dom4j.ElementPath; 16 import org.dom4j.io.SAXReader; 17 18 25 public class LargeDocumentDemo extends SAXDemo implements ElementHandler { 26 27 protected String pruningPath; 28 29 public static void main(String [] args) { 30 run(new LargeDocumentDemo(), args); 31 } 32 33 public LargeDocumentDemo() { 34 } 35 36 public void run(String [] args) throws Exception { 37 if (args.length < 2) { 38 printUsage("<XML document URL> <pruningPath>"); 39 return; 40 } 41 42 String xmlFile = args[0]; 43 pruningPath = args[1]; 44 45 Document document = parse(xmlFile); 46 process(document); 47 } 48 49 public void onStart(ElementPath path) { 51 Element element = path.getCurrent(); 52 println("onStart: of parsing element: " + element); 53 } 54 55 public void onEnd(ElementPath path) { 56 Element element = path.getCurrent(); 57 58 println("onEnd: of parsing element: " + element + " with: " 59 + element.content().size() + " content node(s)"); 60 61 element.detach(); 63 } 64 65 protected Document parse(String url) throws Exception { 66 SAXReader reader = new SAXReader(); 67 68 println("Parsing document: " + url); 69 println("Using Pruning Path: " + pruningPath); 70 71 reader.addHandler(pruningPath, this); 73 74 println("##### starting parse"); 75 Document document = reader.read(url); 76 println("##### finished parse"); 77 78 println("Now lets dump what is left of the document after pruning..."); 80 81 return document; 82 } 83 } 84 85 123 | Popular Tags |