xiaoqi376847932 发表于 2013-1-26 13:24:58

Digerster类的API

参考网站:http://blog.csdn.net/fulianglove/archive/2008/07/09/2627472.aspx
01.<!--********* foo.xml文件的内容 *************-->
02.
03.
04.
05.<foo-catgory>
06.
07.<foo name="foo1" count="10">
08.
09.<bar id="1" title="The First Child"/>
10.
11.<bar id="2" title="The Second Child"/>
12.
13.</foo>
14.
15.
16.
17.<foo name="foo2" count="20">
18.
19.<bar id="3" title="foo2 The First Child"/>
20.
21.<bar id="4" title="f002 The Second Child"/>
22.
23.</foo>
24.
25.</foo-catgory>
<!--********* foo.xml文件的内容 *************-->



<foo-catgory>

<foo name="foo1" count="10">

<bar id="1" title="The First Child"/>

<bar id="2" title="The Second Child"/>

</foo>



<foo name="foo2" count="20">

<bar id="3" title="foo2 The First Child"/>

<bar id="4" title="f002 The Second Child"/>

</foo>

</foo-catgory>view plaincopy to clipboardprint?
01.////**********************class Bar ****************   
02.
03.package mypackage;   
04.
05.
06.
07./**   
08.
09.* <p>Titleigester类的api进行xml解析的简单例子 </p>   
10.
11.*   
12.
13.* <p>Description:对应于xml中的<foo-catgory>下的<foo>下<bar>元素 </p>   
14.
15.*   
16.
17.* <p>Copyright: Copyright (c) 2005</p>   
18.
19.*   
20.
21.* <p>Company: </p>   
22.
23.*   
24.
25.* @author wdz123@hotmail.com   
26.
27.* @version 1.0
28.
29.*/   
30.
31.public class Bar {   
32.
33.private int id;   
34.
35.private String title;   
36.
37.public int getId() {   
38.
39.return id;   
40.
41.}   
42.
43.public void setId(int id) {   
44.
45.this.id = id;   
46.
47.}   
48.
49.public String getTitle() {   
50.
51.return title;   
52.
53.}   
54.
55.public void setTitle(String title) {   
56.
57.this.title = title;   
58.
59.}   
60.
61.private void prints(String s) {   
62.
63.System.out.println(" " + s);   
64.
65.}   
66.
67.public void print(String s) {   
68.
69.prints(s + "---Bar.id=" + id + ",Bar.title=" + title);   
70.
71.}   
72.
73.}
////**********************class Bar ****************

package mypackage;



/**

* <p>Titleigester类的api进行xml解析的简单例子 </p>

*

* <p>Description:对应于xml中的<foo-catgory>下的<foo>下<bar>元素 </p>

*

* <p>Copyright: Copyright (c) 2005</p>

*

* <p>Company: </p>

*

* @author wdz123@hotmail.com

* @version 1.0

*/

public class Bar {

private int id;

private String title;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getTitle() {

return title;

}

public void setTitle(String title) {

this.title = title;

}

private void prints(String s) {

System.out.println(" " + s);

}

public void print(String s) {

prints(s + "---Bar.id=" + id + ",Bar.title=" + title);

}

}view plaincopy to clipboardprint?
01.//****** * class Foo 的代码 ************   
02.
03.
04.
05.package mypackage;   
06.
07.
08.
09.import java.util.*;   
10.
11.
12.
13./**   
14.
15.* <p>Titleigester类的api进行xml解析的简单例子 </p>   
16.
17.*   
18.
19.* <p>Description:对应于xml中的<foo-catgory>下的<foo>元素 </p>   
20.
21.*   
22.
23.* <p>Copyright: Copyright (c) 2005</p>   
24.
25.*   
26.
27.* <p>Company: </p>   
28.
29.*   
30.
31.* @author wdz123@hotmail.com   
32.
33.* @version 1.0
34.
35.*/   
36.
37.public class Foo {   
38.
39.private HashMap list;   
40.
41.private String name;   
42.
43.private int count;   
44.
45.public void addBar(Bar bar) {   
46.
47.list.put(new Integer(bar.getId()), bar);   
48.
49.}   
50.
51.
52.
53.public Bar findBar(int id) {   
54.
55.return (Bar) (list.get(new Integer(id)));   
56.
57.}   
58.
59.public Iterator getBars() {   
60.
61.return
62.
63.list.keySet().iterator();   
64.
65.}   
66.
67.public String getName() {   
68.
69.return name;   
70.
71.}   
72.
73.public int getCount() {   
74.
75.return count;   
76.
77.}   
78.
79.public void setName(String n) {   
80.
81.this.name = n;   
82.
83.}   
84.
85.public void setCount(int count) {   
86.
87.this.count = count;   
88.
89.}   
90.
91.public Foo() {   
92.
93.super();   
94.
95.list = new HashMap();   
96.
97.}   
98.
99.
100.
101.public void print(){   
102.
103.Iterator i = getBars();   
104.
105.Bar b;   
106.
107.int c = 0;   
108.
109.while (i.hasNext()) {   
110.
111.Integer ii = (Integer) i.next();   
112.
113.b = findBar(ii.intValue());   
114.
115.b.print(" Bar" + c++ +"---");   
116.
117.}   
118.
119.}   
120.
121.}   
122.<PRE class=java name="code">///*******************class FooCatgory 的代码 **************   
123.
124.package mypackage;   
125.
126.
127.
128.import java.util.LinkedList;   
129.
130.import java.util.Iterator;   
131.
132.
133.
134./**   
135.
136.* <p>Title: Digester类的api进行xml解析的简单例子</p>   
137.
138.*   
139.
140.* <p>Description: :对应于xml文件中的<foo-catgory>元素 </p>   
141.
142.*   
143.
144.* <p>Copyright: Copyright (c) 2005</p>   
145.
146.*   
147.
148.* <p>Company: </p>   
149.
150.*   
151.
152.* @author wdz123@hotmail.com   
153.
154.* @version 1.0
155.
156.*/   
157.
158.public class FooCatgory {   
159.
160.LinkedList list = new LinkedList();   
161.
162.
163.
164.public void addFoo(Foo p) {   
165.
166.list.addLast(p);   
167.
168.}   
169.
170.private void prints(String s) {   
171.
172.System.out.println(" " + s);   
173.
174.}   
175.
176.
177.
178.public void print() {   
179.
180.prints("FooCatgory has " + list.size() + " elements");   
181.
182.int c = 0; //   
183.
184.Foo f;   
185.
186.for (Iterator i = list.iterator(); i.hasNext(); ) {   
187.
188.f = (Foo) i.next();   
189.
190.prints(" Foo" + c++ +"---" + "Foo.name=" + f.getName() + ",Foo.count=" +   
191.
192.f.getCount());   
193.
194.f.print();   
195.
196.}   
197.
198.}   
199.
200.}</PRE>   
201.<PRE class=java name="code"><PRE class=java name="code">*********** class ParseXmlFile 的代码 *******************   
202.
203.
204.
205.package mypackage;   
206.
207.
208.
209.import org.apache.commons.digester.*;   
210.
211.
212.
213./**
214.
215.* <p>Titleigester类的api进行xml解析的简单例子 </p>
216.
217.*
218.
219.* <p>Description: 使用Digester的api方法进行具体的解析xml文件,得到相应的java对象</p>
220.
221.*
222.
223.* <p>Copyright: Copyright (c) 2005</p>
224.
225.*
226.
227.* <p>Company: </p>
228.
229.*
230.
231.* @author wdz123@hotmail.com
232.
233.* @version 1.0
234.
235.*/
236.
237.public class ParseXmlFile {   
238.
239.public ParseXmlFile() {   
240.
241.super();   
242.
243.}   
244.
245.
246.
247.private void parse(String sFileName) {   
248.
249.Digester d = new Digester();   
250.
251.//不进行XML与相应的DTD的合法性验证,如果设置=true,那应该有对应的dtd文件,并且在xml文件指出使用的dtd文件   
252.
253.//digester.setValidating(false); //   
254.
255.
256.
257.//创建根对象   
258.
259.FooCatgory fooCatgory = new FooCatgory();   
260.
261.
262.
263.//根对象进入对象栈(入栈,成为栈顶元素)   
264.
265.d.push(fooCatgory);   
266.
267.
268.
269.// 当遇到<foo>时创建一个mypackage.Foo对象,并且进入对象栈(入栈,成为栈顶元素)   
270.
271.d.addObjectCreate("foo-catgory/foo", mypackage.Foo.class);   
272.
273.
274.
275.// 根据<foo>元素的属性(attribute),对刚才创建的Foo对象的属性(property)进行设置   
276.
277.d.addSetProperties("foo-catgory/foo");   
278.
279.
280.
281.//当再次遇到<foo>的子元素<bar>时创建一个mypackage.Bar对象,并将其放在栈顶,同时父元素(fooCatgory对象)的addFoo方法。   
282.
283.d.addSetNext("foo-catgory/foo", "addFoo");   
284.
285.
286.
287.// 当遇到<bar>时创建一个mypackage.Bar对象,并将其放在栈顶(入栈,成为栈顶元素)   
288.
289.d.addObjectCreate("foo-catgory/foo/bar", mypackage.Bar.class);   
290.
291.
292.
293.// 根据<bar>元素的属性(attribute),对刚创建的Bar对象的属性(property)进行设置   
294.
295.d.addSetProperties("foo-catgory/foo/bar");   
296.
297.
298.
299.//当再次遇到<bar>时创建一个mypackage.Bar对象,并将其放在栈顶,同时调用父元素(Foo对象)的addBar方法。   
300.
301.d.addSetNext("foo-catgory/foo/bar", "addBar", "mypackage.Bar");   
302.
303.
304.
305.try {   
306.
307.//开始解析Xml-- foo.xml 该文件必须放到project根目录下,和目录src同级目录   
308.
309.d.parse(sFileName);   
310.
311.}   
312.
313.catch (java.io.IOException ioe) {   
314.
315.System.out.println("IOException reading input file:" + ioe.getMessage());   
316.
317.System.exit( -1);   
318.
319.}   
320.
321.catch (org.xml.sax.SAXException se) {   
322.
323.System.out.println("SAXException Error parsing input file:" +   
324.
325.se.getMessage());   
326.
327.System.exit( -1);   
328.
329.}   
330.
331.catch (Exception ex) {   
332.
333.System.out.println("Exception parse error!" + ex.getMessage());   
334.
335.}   
336.
337.
338.
339.//打印xml文件解析的结果   
340.
341.fooCatgory.print();   
342.
343.}   
344.
345.
346.
347.public static void main(String[] args) {   
348.
349.ParseXmlFile p = new ParseXmlFile();   
350.
351.p.parse("foo.xml");   
352.
353.}   
354.
355.}</PRE>   
356.</PRE>
页: [1]
查看完整版本: Digerster类的API