大家好,欢迎来到IT知识分享网。
这篇文章主要是为了写Mybatis源码解析配置文件xml所需的背景知识,
表达式部分的文档
https://www.w3.org/TR/1999/REC-xpath-19991116/
JAVA JSR 206: Java API for XML Processing (JAXP) 1.3
https://docs.oracle.com/cd/E17802_01/webservices/webservices/docs/2.0/jaxp/ReleaseNotes.html#migrationFrom13
先看下这个是xml文件,然后看这个文档如何解析
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="org.apache.ibatis.begincode.Mapper"> <insert id="insertUser"> insert into users (id, name) values (#{id}, #{name}) </insert> <insert id="insertUser2"> insert into users (id, name) values (#{id}, #{name}) </insert> <select id="selectUser" resultType="org.apache.ibatis.begincode.User"> select * from users </select> </mapper>
读文件的demo
/** * Copyright 2009-2022 the original author or authors. * <p> * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * <p> * http://www.apache.org/licenses/LICENSE-2.0 * <p> * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.ibatis.begincode; import org.apache.ibatis.parsing.XPathParser; import org.junit.jupiter.api.Test; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.EntityResolver; import org.xml.sax.SAXException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class XMLTest { // https://www.w3.org/TR/1999/REC-xpath-/ @Test public void resolveXml() throws ParserConfigurationException, IOException, SAXException, XPathExpressionException { String fileName = "Mapper.xml"; String xmlFilePath = this.getClass().getResource("").getPath() + fileName; DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setValidating(false); DocumentBuilder db = documentBuilderFactory.newDocumentBuilder(); Document doc = db.parse(new FileInputStream(new File(xmlFilePath))); XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); String expression; Node node; NodeList nodeList; // 1. 根目录 expression = "/*"; node = (Node) xpath.evaluate(expression, doc, XPathConstants.NODE); System.out.println("1. " + node.getNodeName()); // 2.根据标签名获取根目录 expression = "/mapper"; node = (Node) xpath.evaluate(expression, doc, XPathConstants.NODE); System.out.println("2. " + node.getNodeName()); // 3. 根据标签层级获取节点 expression = "/mapper/select"; node = (Node) xpath.evaluate(expression, doc, XPathConstants.NODE); System.out.println("3. " + node.getNodeName()); // 4. 获取标签下所有节点 expression = "/mapper/*"; nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET); System.out.print("4. "); for (int i = 0; i < nodeList.getLength(); i++) { System.out.print(nodeList.item(i).getNodeName() + " "); } System.out.println(); // 5. 获取所有指定节点 expression = "//insert"; nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET); System.out.print("5. "); for (int i = 0; i < nodeList.getLength(); i++) { System.out.print(nodeList.item(i).getNodeName() + " "); } System.out.println(); // 6. 获取所有非指定名字节点 expression = "//*[name() != 'insert']"; nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET); System.out.print("6. "); for (int i = 0; i < nodeList.getLength(); i++) { System.out.print(nodeList.item(i).getNodeName() + " "); } System.out.println(); // 7. 获取至少有一个子节点的节点 expression = "//*[*]"; nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET); System.out.print("7. "); for (int i = 0; i < nodeList.getLength(); i++) { System.out.print(nodeList.item(i).getNodeName() + " "); } System.out.println(); // 8.获取指定层级的节点 expression = "/*/*"; nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET); System.out.print("8. "); for (int i = 0; i < nodeList.getLength(); i++) { System.out.print(nodeList.item(i).getNodeName() + " "); System.out.print(nodeList.item(i).getFirstChild().getNodeValue() + " "); } System.out.println(); } }
执行结果
其他使用方法 ,直接用 Document处理
@Test public void documentTest() throws ParserConfigurationException, IOException, SAXException { String fileName = "Mapper.xml"; String xmlFilePath = this.getClass().getResource("").getPath() + fileName; DocumentBuilderFactory docbuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dombuilder = docbuilderFactory.newDocumentBuilder(); InputStream is = new FileInputStream(xmlFilePath); Document doc = dombuilder.parse(is); Element root = doc.getDocumentElement(); System.out.println(root.getChildNodes().item(1).getFirstChild().getNodeValue()); }
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/52964.html