龙空技术网

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

BeginCode 3156

前言:

今天朋友们对“jsxmldocument”大概比较关心,同学们都想要剖析一些“jsxmldocument”的相关文章。那么小编也在网摘上搜集了一些对于“jsxmldocument””的相关文章,希望我们能喜欢,姐妹们快快来学习一下吧!

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

表达式部分的文档  JSR 206: Java API for XML Processing (JAXP) 1.3

先看下这个是xml文件,然后看这个文档如何解析

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  ";><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.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 {//    @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处理

@Testpublic 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());}

标签: #jsxmldocument