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 24 public class LargeDocumentDemo2 extends SAXDemo { 25 26 public static void main(String [] args) { 27 run(new LargeDocumentDemo2(), args); 28 } 29 30 public LargeDocumentDemo2() { 31 } 32 33 public void run(String [] args) throws Exception { 34 if (args.length < 1) { 35 printUsage("<XML document URL>"); 36 return; 37 } 38 39 String xmlFile = args[0]; 40 41 Document document = parse(xmlFile); 42 process(document); 43 } 44 45 protected Document parse(String url) throws Exception { 46 SAXReader reader = new SAXReader(); 47 48 println("Parsing document: " + url); 49 50 reader.addHandler("/PLAY/ACT", new playActHandler()); 52 53 println("##### starting parse"); 54 Document document = reader.read(url); 55 println("##### finished parse"); 56 57 println("Now lets dump what is left of the document after pruning..."); 59 60 return document; 61 } 62 63 class playActHandler implements ElementHandler { 64 public void onStart(ElementPath path) { 65 Element element = path.getCurrent(); 66 path.addHandler("SCENE/SPEECH", new actSceneSpeechHandler()); 67 } 68 69 public void onEnd(ElementPath path) { 70 Element element = path.getCurrent(); 71 println("Found Act: " + element.element("TITLE").getText()); 72 path.removeHandler("SCENE/SPEECH"); 73 element.detach(); 74 } 75 } 76 77 class actSceneSpeechHandler implements ElementHandler { 78 public void onStart(ElementPath path) { 79 Element element = path.getCurrent(); 80 println("Found Start of Speech"); 81 } 82 83 public void onEnd(ElementPath path) { 84 Element element = path.getCurrent(); 85 println("Found End of Speech by Speaker: " 86 + element.element("SPEAKER").getText()); 87 element.detach(); 88 } 89 } 90 } 91 92 130 | Popular Tags |