/Users/lyon/j4p/src/xml/musicCatalog/MyDomParser.java

1    /** 
2     * MyDomParser.java 
3     * @author Thomas Rowland 
4     * @version 02-08-03 
5     */ 
6    package xml.musicCatalog; 
7     
8    import org.apache.xerces.parsers.DOMParser; 
9    import org.w3c.dom.*; 
10   import org.xml.sax.SAXException; 
11    
12   import java.io.IOException; 
13   import java.io.OutputStream; 
14   import java.io.PrintStream; 
15    
16   /* 
17    * This program demonstrates how to parse an XML file using the 
18    * Xerces parser implementation directly, and the DOM api. 
19    * Does not use JAXP. 
20    * Parses a MusicCatalog XML file and prints out the content. 
21    */ 
22    
23   public class MyDomParser { 
24    
25       private static DOMParser parser = null; 
26       private static PrintStream out = null; 
27    
28       static final int ELEMENT_NODE = 1; 
29       static final int ATTR_NODE = 2; 
30       static final int TEXT_NODE = 3; 
31    
32       /* 
33        *  Parse an xml file. 
34        */ 
35       public static void parse(String uri, OutputStream os) 
36               throws Exception { 
37    
38           try { 
39               out = new PrintStream(os); 
40               parser = new DOMParser(); 
41               parser.parse(uri); 
42               Document domDocument = parser.getDocument(); 
43               Element root = domDocument.getDocumentElement(); 
44               traverse(root); 
45               out.flush(); 
46           } catch (SAXException e) { 
47               out.flush(); 
48               // get the wrapped exception 
49               Exception ex = e.getException(); 
50               String msg = null; 
51               if (ex != null) { 
52                   ex.printStackTrace(); 
53                   msg = ex.getMessage(); 
54               } 
55               throw new Exception( 
56                       "** SAXException:\n" + e.getMessage()); 
57           } catch (IOException e) { 
58               out.flush(); 
59               throw new Exception( 
60                       "IOException:\n" + e.getMessage()); 
61           } 
62       } 
63    
64    
65       /* 
66        * Recursive method which traverses the DOM Document 
67        * and outputs the content to the specified OutputStream. 
68        */ 
69       public static void traverse(Node elem) { 
70           try { 
71               // handle the attributes 
72               if (elem.hasAttributes()) { 
73                   NamedNodeMap attrs = elem.getAttributes(); 
74                   int alength = attrs.getLength(); 
75                   for (int i = 0; i < alength; i++) { 
76                       Attr attr = (Attr) attrs.item(i); 
77                       out.println(attr.getName() + ":\t" + attr.getValue()); 
78                   } 
79               } 
80    
81               // handle the child nodes, printing out <Item> elements 
82               if (elem.hasChildNodes()) { 
83                   NodeList children = elem.getChildNodes(); 
84                   int length = children.getLength(); 
85    
86                   for (int i = 0; i < length; i++) { 
87                       Node n = children.item(i); 
88                       String name = n.getNodeName(); 
89    
90                       if (n.getNodeType() == ELEMENT_NODE) { 
91                           if (name.equals("Item")) 
92                               out.println("\n"); 
93                           else 
94                               out.print(name + ":\t"); 
95                           traverse(n); //recurse 
96                       } else if (n.getNodeType() == TEXT_NODE) { 
97                           String txt = n.getNodeValue().trim(); 
98                           if (!txt.equals("")) { 
99                               out.println(txt); 
100                          } 
101                      } 
102                  } 
103                  return; 
104              } 
105          } catch (DOMException e) { 
106              System.out.println( 
107                      "*** DOMException\n" + e.getMessage()); 
108          } 
109      } 
110   
111  }//