• 【Java】XML文件的解析


      1 import java.io.File;
      2 import java.io.FileInputStream;
      3 import java.io.FileNotFoundException;
      4 import java.io.IOException;
      5 import java.io.InputStream;
      6 
      7 import javax.xml.parsers.DocumentBuilder;
      8 import javax.xml.parsers.DocumentBuilderFactory;
      9 import javax.xml.parsers.ParserConfigurationException;
     10 
     11 import org.w3c.dom.Document;  
     12 import org.w3c.dom.Element;
     13 import org.w3c.dom.NodeList;
     14 import org.xml.sax.SAXException;
     15 
     16 public abstract class XMLReader {
     17     // 只需要一份 documentBuilder
     18     private static DocumentBuilder documentBuilder;
     19     
     20     static {
     21         try {
     22             documentBuilder = DocumentBuilderFactory
     23                     .newInstance()
     24                     .newDocumentBuilder();
     25         } catch (ParserConfigurationException e) {
     26             e.printStackTrace();
     27         }
     28     }
     29     
     30     public XMLReader() {
     31     }
     32     
     33     // 获取Document,Document是w3c的,不要导错了
     34     public static Document openDocument(InputStream is) {
     35         Document document = null;
     36         try {
     37             document = documentBuilder.parse(is);
     38         } catch (SAXException | IOException e) {
     39             e.printStackTrace();
     40         } finally {
     41             try {
     42                 is.close();
     43             } catch (IOException e) {
     44                 e.printStackTrace();
     45             }
     46         }
     47         
     48         return document;
     49     }
     50 
     51     public static Document openDocument(String xmlPath) {
     52         InputStream is = XMLReader.class.getResourceAsStream(xmlPath);
     53         if (is == null) {
     54             try {
     55                 // 自定义异常 
     56                 throw new XMLFileNotExistException("文件[" + xmlPath+ "]不存在!");
     57             } catch (XMLFileNotExistException e) {
     58                 e.printStackTrace();
     59             }
     60         } 
     61         
     62         return openDocument(is);
     63     }
     64     
     65     public static Document openDocument(File path) {
     66         InputStream is = null;
     67         try {
     68             is = new FileInputStream(path);
     69         } catch (FileNotFoundException e) {
     70             e.printStackTrace();
     71         }
     72         return openDocument(is);
     73     }
     74     
     75     // 抽象方法,由用户自行处理得到的标签
     76     public abstract void dealElment(Element element, int index);
     77     
     78     // 抽象方法在for循环中,每次读取到一个标签,就交给抽象方法,由用户自行使用
     79     public XMLReader parse(Element element, String tagname) {
     80         NodeList nodeList = element.getElementsByTagName(tagname);
     81         
     82         for (int index = 0; index < nodeList.getLength(); index++) {
     83             Element ele = (Element) nodeList.item(index);
     84             
     85             dealElment(ele, index);
     86         }
     87         
     88         return this;
     89     }
     90     
     91     public XMLReader parse(Document document, String tagname) {
     92         NodeList nodeList = document.getElementsByTagName(tagname);
     93         
     94         for (int index = 0; index < nodeList.getLength(); index++) {
     95             Element element = (Element) nodeList.item(index);
     96             
     97             dealElment(element, index);
     98         }
     99         
    100         return this;
    101     }
    102     
    103 }

    这是我们的XML文件,对其进行解析

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <Test>
     3     <class name = "第一个标签" type = "firstLable">
     4         <parameter id = "one" value = "一"></parameter>
     5         <parameter id = "two" value = "二"></parameter>
     6         <parameter id = "thr" value = "三"></parameter>
     7     </class>
     8     <class name = "第二个标签" type = "secondLable">
     9         <parameter id = "four" value = "四"></parameter>
    10         <parameter id = "six" value = "五"></parameter>
    11     </class>
    12 </Test>
    public static void main(String[] args) {
        new XMLReader() {
            
            @Override
            public void dealElment(Element element, int index) {
                String name = element.getAttribute("name");
                String type = element.getAttribute("type");
                
                System.out.println("name=" + name + "  type=" + type);
                
                new XMLReader() {
                    
                    @Override
                    public void dealElment(Element element, int index) {
                        String id = element.getAttribute("id");
                        String value = element.getAttribute("value");
                        
                        System.out.println("	id=" + id + "  value=" + value);
                    }
                }.parse(element, "parameter");
            }
        }.parse(XMLReader.openDocument("/test.xml"), "class");
    }

    结果如下

  • 相关阅读:
    MongoDB之Limit及Skip方法
    MongoDB之$type操作符
    MongoDB之条件操作符
    MongoDB之文档的增删改查
    MongoDB之集合的创建与删除
    MongoDB之数据库的创建及删除
    MongoDB之术语解析
    很少用的U盘,今天居然无法打开(插入盘后能看到盘符但是无法打开的问题)
    IDEA安装后必须设置的选项
    IDEA2020离线更新迭代小版本
  • 原文地址:https://www.cnblogs.com/a526583280/p/9745491.html
Copyright © 2020-2023  润新知