Dom解析EntityResolver以及mybatis解析xml

EntityResolver

我们都知道,xml一般都有一份声明,是用来规范xml标签语法的,EntityResolver的作用就是自定义验证或者(不验证)这份声明。
xml解析过程中会首先读取声明,然后根据声明的链接去找远程dtd定义,如果网络不好这个过程可能会报错,所以一般我们是将dtd定义放到本地,然后就可以通过注册一个实现了EntityResolver的自定义解析器去解析本地的dtd定义,排除网络影响。
mybatis解析xml也是这样做的

EntityResolver使用

xml



<mapper namespace="userMapper">

    <resultMap id="personMap" type="org.apache.ibatis.test.User">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
    resultMap>

    <sql id="selectByIdScript">
        SELECT id, `name`
        FROM user
        WHERE name = #{name}
    sql>
    <select id="selectUserByName" resultMap="personMap" parameterType="String">
        <include refid="selectByIdScript"/>
    select>
mapper>
DomEntityResolver 实现了EntityResolver

DomEntityResolver定义了几个常量是dtd文件存放的路径,通过重写resolveEntity方法,在dom解析xml的时候读取到声明就会调用我们重写的这个方法,然后判断是config还是mapper去调用classloader读取dtd文件,这里publicId与systemId分别如下
publicId:"-//mybatis.org//DTD Mapper 3.0//EN"
systemId:“http://mybatis.org/dtd/mybatis-3-mapper.dtd”

public class DomEntityResolver implements EntityResolver {

    private static final String MYBATIS_CONFIG_SYSTEM = "mybatis-3-config.dtd";
    private static final String MYBATIS_MAPPER_SYSTEM = "mybatis-3-mapper.dtd";

    private static final String MYBATIS_CONFIG_DTD = "mybatis-3-config.dtd";
    private static final String MYBATIS_MAPPER_DTD = "mybatis-3-mapper.dtd";


    public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
        try {
            if (systemId != null) {
                String lowerCaseSystemId = systemId.toLowerCase(Locale.ENGLISH);
                if (lowerCaseSystemId.contains(MYBATIS_CONFIG_SYSTEM)) {
                    return getInputSource(MYBATIS_CONFIG_DTD, publicId, systemId);
                } else if (lowerCaseSystemId.contains(MYBATIS_MAPPER_SYSTEM) ) {
                    return getInputSource(MYBATIS_MAPPER_DTD, publicId, systemId);
                }
            }
            return null;
        } catch (Exception e) {
            throw new SAXException(e.toString());
        }
    }

    private InputSource getInputSource(String path, String publicId, String systemId) {
        InputSource source = null;
        if (path != null) {
            try {
                InputStream in = DomEntityResolver.class.getClassLoader().getResourceAsStream(path);
                source = new InputSource(in);
                source.setPublicId(publicId);
                source.setSystemId(systemId);
            } catch (Exception e) {
                // ignore, null is ok
            }
        }
        return source;
    }
}
模仿mybatis解析xml

java原生Dom解析,关键是要获取xml的Document,会有这四步

  1. 获取DocumentBuilderFactor工厂:
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  2. 通过工厂获取DocumentBuidler构建者:
    DocumentBuilder builder = factory.newDocumentBuilder();
  3. 通过DocumentBuidler获取Document:
    Document doc = builder.parse(“xml文件的相对路径或者绝对路径”);
  4. getElementsByTagName获取element元素
    doc.getElementsByTagName(“select”)
public class DomParser {

    public static void main(String[] args) throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        // 注册EntityResolver
        builder.setEntityResolver(new DomEntityResolver());
        Document doc = builder.parse(DomParser.class.getClassLoader().getResource("User.xml").getFile());
        System.out.println(doc.getDocumentElement().getNodeName());
        // 解析mybatis的resultMap标签
        NodeList mapper = doc.getElementsByTagName("resultMap");
        for (int i = 0; i < mapper.getLength(); i++) {
            Element item = (Element)mapper.item(i);
            NamedNodeMap attributes = item.getAttributes();
            System.out.println(item.getNodeName());
            System.out.print("attributes ");
            for (int l = 0; l < attributes.getLength(); l++) {
                System.out.print(attributes.item(l).getNodeName() + "-" + attributes.item(l).getNodeValue() + " ");
            }
            System.out.println();
            NodeList result = item.getElementsByTagName("result");
            System.out.println("result property-"
                    + ((Element) result.item(0)).getAttribute("property")
                    + " column-" + ((Element) result.item(0)).getAttribute("column"));
        }
        // 解析mybatis的select标签
        NodeList select = doc.getElementsByTagName("select");
        System.out.println("select");
        for (int i = 0; i < select.getLength(); i++) {
            Element item = (Element) select.item(i);
            System.out.println("id-" + item.getAttribute("id") + "\n"
                    + "resultMap-" + item.getAttribute("resultMap") +"\n"
                    + "parameterType-" + item.getAttribute("parameterType"));
        }
        // 解析mybatis的sql标签
        NodeList sql = doc.getElementsByTagName("sql");
        System.out.println("========sql===========" + sql.item(0).getFirstChild().getNodeValue());
    }
}

Dom解析EntityResolver以及mybatis解析xml_第1张图片
我们可以看到,已经将mapper的属性,sql内容等全部都解析出来了

你可能感兴趣的:(Dom解析EntityResolver以及mybatis解析xml)