xuxiuxi
2017-07-22 4bee2fa9f76f7e200bf1bf01c24b709d16a58488
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.awsle.aibatis.xml.engine.xml.dom;
 
import com.awsle.aibatis.xml.engine.xml.CannotParseXMLException;
import com.awsle.aibatis.xml.engine.xml.XMLReader;
import com.awsle.aibatis.xml.engine.xml.XMLReaderDriver;
 
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
 
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
 
public class DomXMLReaderDriver implements XMLReaderDriver {
 
    public XMLReader createReader(String xml) {
        try {
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            ByteArrayInputStream inputStream = new ByteArrayInputStream(xml.getBytes());
            Document document = documentBuilder.parse(inputStream);
            return new DomXMLReader(document);
        } catch (FactoryConfigurationError e) {
            throw new CannotParseXMLException(e);
        } catch (ParserConfigurationException e) {
            throw new CannotParseXMLException(e);
        } catch (SAXException e) {
            throw new CannotParseXMLException(e);
        } catch (IOException e) {
            throw new CannotParseXMLException(e);
        }
    }
 
}