KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dom4j > samples > LargeDocumentDemo


1 /*
2  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
3  *
4  * This software is open source.
5  * See the bottom of this file for the licence.
6  *
7  * $Id: LargeDocumentDemo.java,v 1.4 2005/01/29 14:52:57 maartenc Exp $
8  */

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 /**
19  * This sample parses a big document using the pruning option of the
20  * {@link SAXReader}.
21  *
22  * @author <a HREF="mailto:james.strachan@metastuff.com">James Strachan </a>
23  * @version $Revision: 1.4 $
24  */

25 public class LargeDocumentDemo extends SAXDemo implements ElementHandler {
26
27     protected String JavaDoc pruningPath;
28
29     public static void main(String JavaDoc[] args) {
30         run(new LargeDocumentDemo(), args);
31     }
32
33     public LargeDocumentDemo() {
34     }
35
36     public void run(String JavaDoc[] args) throws Exception JavaDoc {
37         if (args.length < 2) {
38             printUsage("<XML document URL> <pruningPath>");
39             return;
40         }
41
42         String JavaDoc xmlFile = args[0];
43         pruningPath = args[1];
44
45         Document document = parse(xmlFile);
46         process(document);
47     }
48
49     // ElementHandler interface
50
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         // now prune the current element to reduce memory
62
element.detach();
63     }
64
65     protected Document parse(String JavaDoc url) throws Exception JavaDoc {
66         SAXReader reader = new SAXReader();
67
68         println("Parsing document: " + url);
69         println("Using Pruning Path: " + pruningPath);
70
71         // enable pruning to call me back as each Element is complete
72
reader.addHandler(pruningPath, this);
73
74         println("##### starting parse");
75         Document document = reader.read(url);
76         println("##### finished parse");
77
78         // the document will be complete but have the prunePath elements pruned
79
println("Now lets dump what is left of the document after pruning...");
80
81         return document;
82     }
83 }
84
85 /*
86  * Redistribution and use of this software and associated documentation
87  * ("Software"), with or without modification, are permitted provided that the
88  * following conditions are met:
89  *
90  * 1. Redistributions of source code must retain copyright statements and
91  * notices. Redistributions must also contain a copy of this document.
92  *
93  * 2. Redistributions in binary form must reproduce the above copyright notice,
94  * this list of conditions and the following disclaimer in the documentation
95  * and/or other materials provided with the distribution.
96  *
97  * 3. The name "DOM4J" must not be used to endorse or promote products derived
98  * from this Software without prior written permission of MetaStuff, Ltd. For
99  * written permission, please contact dom4j-info@metastuff.com.
100  *
101  * 4. Products derived from this Software may not be called "DOM4J" nor may
102  * "DOM4J" appear in their names without prior written permission of MetaStuff,
103  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
104  *
105  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
106  *
107  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
108  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
109  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
110  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
111  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
112  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
113  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
114  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
115  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
116  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
117  * POSSIBILITY OF SUCH DAMAGE.
118  *
119  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
120  *
121  * $Id: LargeDocumentDemo.java,v 1.4 2005/01/29 14:52:57 maartenc Exp $
122  */

123
Popular Tags