1 17 18 19 20 package org.apache.lenya.xml.parser; 21 22 import java.io.InputStream ; 23 import java.io.PrintWriter ; 24 import java.io.Reader ; 25 26 import org.apache.lenya.xml.DOMWriter; 27 import org.apache.xerces.dom.CDATASectionImpl; 28 import org.apache.xerces.dom.CommentImpl; 29 import org.apache.xerces.dom.DocumentImpl; 30 import org.apache.xerces.dom.ElementImpl; 31 import org.apache.xerces.dom.TextImpl; 32 import org.apache.xerces.parsers.DOMParser; 33 import org.w3c.dom.CDATASection ; 34 import org.w3c.dom.Comment ; 35 import org.w3c.dom.Document ; 36 import org.w3c.dom.Element ; 37 import org.w3c.dom.Text ; 38 39 40 43 public class XercesParser implements Parser { 44 49 public static void main(String [] args) { 50 Parser parser = new XercesParser(); 51 52 if (args.length != 1) { 53 System.err.println("Usage: java " + parser.getClass().getName() + " example.xml"); 54 55 return; 56 } 57 58 Document doc = null; 59 60 try { 61 doc = parser.getDocument(args[0]); 62 } catch (Exception e) { 63 System.err.println(e); 64 } 65 66 new DOMWriter(new PrintWriter (System.out)).print(doc); 67 System.out.println(""); 68 69 Document document = parser.getDocument(); 70 Element michi = parser.newElementNode(document, "Employee"); 71 michi.setAttribute("Id", "michi"); 72 michi.appendChild(parser.newTextNode(document, "Michi")); 73 74 Element employees = parser.newElementNode(document, "Employees"); 75 employees.appendChild(parser.newTextNode(document, "\n")); 76 employees.appendChild(michi); 77 employees.appendChild(parser.newTextNode(document, "\n")); 78 document.appendChild(employees); 79 new DOMWriter(new PrintWriter (System.out)).print(document); 80 System.out.println(""); 81 } 82 83 92 public Document getDocument(String filename) throws Exception { 93 DOMParser parser = new DOMParser(); 94 95 org.xml.sax.InputSource in = new org.xml.sax.InputSource (filename); 96 parser.parse(in); 97 98 return parser.getDocument(); 99 } 100 101 110 public Document getDocument(InputStream is) throws Exception { 111 DOMParser parser = new DOMParser(); 112 org.xml.sax.InputSource in = new org.xml.sax.InputSource (is); 113 parser.parse(in); 114 115 return parser.getDocument(); 116 } 117 118 127 public Document getDocument(Reader reader) throws Exception { 128 DOMParser parser = new DOMParser(); 129 org.xml.sax.InputSource in = new org.xml.sax.InputSource (reader); 130 parser.parse(in); 131 132 return parser.getDocument(); 133 } 134 135 140 public Document getDocument() { 141 return new DocumentImpl(); 142 } 143 144 152 public Element newElementNode(Document document, String name) { 153 return new ElementImpl((DocumentImpl) document, name); 154 } 155 156 165 public Element newElementNSNode(Document document, String namespaceUri, String qualifiedName) { 166 return document.createElementNS(namespaceUri, qualifiedName); 167 } 168 169 177 public Text newTextNode(Document document, String data) { 178 return new TextImpl((DocumentImpl) document, data); 179 } 180 181 189 public CDATASection newCDATASection(Document document, String data) { 190 return new CDATASectionImpl((DocumentImpl) document, data); 191 } 192 193 201 public Comment newCommentNode(Document document, String data) { 202 return new CommentImpl((DocumentImpl) document, data); 203 } 204 } 205 | Popular Tags |