xml文件怎么打开 xml视频格式怎么打开

时间:2023-05-15 01:33/span> 作者:tiger 分类: 新知 浏览:2328 评论:0

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

表达式部分的文档
http://www.w3.org/TR/1999/REC-xpath-19991116/
JAVA  JSR 206: Java API for XML Processing (JAXP) 1.3
http://docs.oracle.com/origin/cd/E17802_01/webservices/webservices/docs/2.0/jaxp/ReleaseNotes.htmlmigrationFrom13

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

<?xml version=&34;1.0&34; encoding=&34;UTF-8&34;?>
<!DOCTYPE mapper
  PUBLIC &34;-//mybatis.org//DTD Mapper 3.0//EN&34;
  &34;http://mybatis.org/dtd/mybatis-3-mapper.dtd&34;>

<mapper namespace=&34;org.apache.ibatis.begincode.Mapper&34;>
  <insert id=&34;insertUser&34;>
    insert into users (id, name) values ({id}, {name})
  </insert>
  <insert id=&34;insertUser2&34;>
    insert into users (id, name) values ({id}, {name})
  </insert>

  <select id=&34;selectUser&34; resultType=&34;org.apache.ibatis.begincode.User&34;>
    select * from users
  </select>
</mapper>

读文件的demo

/**
 * Copyright 2009-2022 the original author or authors.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the &34;License&34;);
 * 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 &34;AS IS&34; 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 {
//  http://www.w3.org/TR/1999/REC-xpath-19991116/
  @Test
  public void resolveXml() throws ParserConfigurationException, IOException, SAXException, XPathExpressionException {
    String fileName = &34;Mapper.xml&34;;
    String xmlFilePath = this.getClass().getResource(&34;&34;).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 = &34;/*&34;;
    node = (Node) xpath.evaluate(expression, doc, XPathConstants.NODE);
    System.out.println(&34;1. &34; + node.getNodeName());

    // 2.根据标签名获取根目录
    expression = &34;/mapper&34;;
    node = (Node) xpath.evaluate(expression, doc, XPathConstants.NODE);
    System.out.println(&34;2. &34; + node.getNodeName());

    // 3. 根据标签层级获取节点

    expression = &34;/mapper/select&34;;
    node = (Node) xpath.evaluate(expression, doc, XPathConstants.NODE);
    System.out.println(&34;3. &34; + node.getNodeName());

    // 4. 获取标签下所有节点
    expression = &34;/mapper/*&34;;
    nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);
    System.out.print(&34;4. &34;);
    for (int i = 0; i < nodeList.getLength(); i++) {
      System.out.print(nodeList.item(i).getNodeName() + &34; &34;);
    }
    System.out.println();

    // 5. 获取所有指定节点
    expression = &34;//insert&34;;
    nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);
    System.out.print(&34;5. &34;);
    for (int i = 0; i < nodeList.getLength(); i++) {
      System.out.print(nodeList.item(i).getNodeName() + &34; &34;);
    }
    System.out.println();

    // 6. 获取所有非指定名字节点
    expression = &34;//*[name() != &39;insert&39;]&34;;
    nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);
    System.out.print(&34;6. &34;);
    for (int i = 0; i < nodeList.getLength(); i++) {
      System.out.print(nodeList.item(i).getNodeName() + &34; &34;);
    }
    System.out.println();

    // 7. 获取至少有一个子节点的节点
    expression = &34;//*[*]&34;;
    nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);
    System.out.print(&34;7. &34;);
    for (int i = 0; i < nodeList.getLength(); i++) {
      System.out.print(nodeList.item(i).getNodeName() + &34; &34;);
    }
    System.out.println();

    // 8.获取指定层级的节点
    expression = &34;/*/*&34;;
    nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);
    System.out.print(&34;8. &34;);
    for (int i = 0; i < nodeList.getLength(); i++) {
      System.out.print(nodeList.item(i).getNodeName() + &34; &34;);
      System.out.print(nodeList.item(i).getFirstChild().getNodeValue() + &34; &34;);
    }
    System.out.println();

  }
}

执行结果

代码执行结果

其他使用方法 ,直接用 Document处理

@Test
public void documentTest() throws ParserConfigurationException, IOException, SAXException {
  String fileName = &34;Mapper.xml&34;;
  String xmlFilePath = this.getClass().getResource(&34;&34;).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());
}

文章评论