您的位置 首页 java

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

这篇文章主要是为了写 Mybatis 源码解析配置文件xml所需的背景知识,

 表达式部分的文档

JAVA  JSR 206: Java API for XML Processing (JAXP) 1.3
https:// DOC s.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"
  "#34;>

<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>
 * 
 * <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 .SAX Exception ;

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  java x.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class XMLTest {
//  
  @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());
}  

文章来源:智云一二三科技

文章标题:JAVA读取XML文件(水文,Mybatis源码)

文章地址:https://www.zhihuclub.com/184776.shtml

关于作者: 智云科技

热门文章

网站地图