KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > io > xml > DomDriver


1 package com.thoughtworks.xstream.io.xml;
2
3 import com.thoughtworks.xstream.io.HierarchicalStreamDriver;
4 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
5 import com.thoughtworks.xstream.io.StreamException;
6 import org.w3c.dom.Document JavaDoc;
7 import org.xml.sax.InputSource JavaDoc;
8 import org.xml.sax.SAXException JavaDoc;
9
10 import javax.xml.parsers.DocumentBuilder JavaDoc;
11 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
12 import javax.xml.parsers.FactoryConfigurationError JavaDoc;
13 import javax.xml.parsers.ParserConfigurationException JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.Reader JavaDoc;
16
17 public class DomDriver implements HierarchicalStreamDriver {
18
19     private String JavaDoc encoding;
20
21     public DomDriver() {
22         encoding = "UTF-8";
23     }
24
25     public DomDriver(String JavaDoc encoding) {
26         this.encoding = encoding;
27     }
28
29     public HierarchicalStreamReader createReader(Reader xml) {
30         try {
31             DocumentBuilderFactory JavaDoc documentBuilderFactory = DocumentBuilderFactory.newInstance();
32             DocumentBuilder JavaDoc documentBuilder = documentBuilderFactory.newDocumentBuilder();
33             Document JavaDoc document = documentBuilder.parse(new InputSource JavaDoc(xml));
34             return new DomReader(document);
35         } catch (FactoryConfigurationError JavaDoc e) {
36             throw new StreamException(e);
37         } catch (ParserConfigurationException JavaDoc e) {
38             throw new StreamException(e);
39         } catch (SAXException JavaDoc e) {
40             throw new StreamException(e);
41         } catch (IOException JavaDoc e) {
42             throw new StreamException(e);
43         }
44     }
45
46 }
47
Popular Tags