JAVA读取XML文件(水文,Mybatis源码)

这篇文章主要是为了写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"?>



  
    insert into users (id, name) values (#{id}, #{name})
  
  
    insert into users (id, name) values (#{id}, #{name})
  

  


读文件的demo

/**
 * Copyright 2009-2022 the original author or authors.
 * 

* 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 *

* http://www.apache.org/licenses/LICENSE-2.0 *

* 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-19991116/ @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());
}
展开阅读全文

页面更新:2024-04-20

标签:源码   文件   层级   根目录   水文   表达式   节点   使用方法   个子   标签   文档

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号

Top