KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > event > Parse


1 package examples.event;
2
3 import com.bea.xml.stream.StaticAllocator;
4 import java.io.FileReader JavaDoc;
5 import javax.xml.stream.*;
6 import javax.xml.stream.events.*;
7 import javax.xml.stream.util.*;
8 import javax.xml.namespace.QName JavaDoc;
9
10 /**
11  * A simple example to iterate over events
12  *
13  * @author Copyright (c) 2002 by BEA Systems. All Rights Reserved.
14  */

15
16 public class Parse {
17   private static String JavaDoc filename = null;
18   
19   private static void printUsage() {
20     System.out.println("usage: java examples.event.Parse <xmlfile>");
21   }
22
23   public static void main(String JavaDoc[] args) throws Exception JavaDoc {
24     try {
25       filename = args[0];
26     } catch (ArrayIndexOutOfBoundsException JavaDoc aioobe){
27       printUsage();
28       System.exit(0);
29     }
30     System.setProperty("javax.xml.stream.XMLInputFactory",
31                        "com.bea.xml.stream.MXParserFactory");
32     System.setProperty("javax.xml.stream.XMLOutputFactory",
33                        "com.bea.xml.stream.XMLOutputFactoryBase");
34     System.setProperty("javax.xml.stream.XMLEventFactory",
35                        "com.bea.xml.stream.EventFactory");
36     
37
38     XMLInputFactory factory = XMLInputFactory.newInstance();
39     XMLEventReader r =
40       factory.createXMLEventReader(new FileReader JavaDoc(filename));
41     while(r.hasNext()) {
42       XMLEvent e = r.nextEvent();
43       System.out.println("ID:"+e.hashCode()+"["+e+"]");
44     }
45   }
46 }
47
Popular Tags