zoukankan      html  css  js  c++  java
  • Java解析XML三种常用方法

    1.使用DOM方式解析:

     1 package com.wzh.dom;
     3 import java.util.Iterator;
     5 import javax.xml.parsers.DocumentBuilder;
     6 import javax.xml.parsers.DocumentBuilderFactory;
     8 import org.w3c.dom.Document;
     9 import org.w3c.dom.Element;
    10 import org.w3c.dom.NamedNodeMap;
    11 import org.w3c.dom.Node;
    12 import org.w3c.dom.NodeList;
    14 public class DomHandler {
    15  
    16     /*
    17      * 解析XML
    18      */
    19     public void read(String fileName) throws Exception {
    20         // 定义工厂API 使应用程序能够从XML文档获取生成DOM对象树的解析器
    21         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    22         // 获取此类的实例之后,将可以从各种输入源解析XML
    23         DocumentBuilder builder = factory.newDocumentBuilder();
    24         // builder.parse(this.getClass().getResourceAsStream("/" + fileName));
    25         // Document接口表示整个HTML或XML文档,从概念上讲,它是文档树的根,并提供对文档数据的基本访问
    26         Document document = builder.parse(this.getClass().getResourceAsStream(
    27                 "/" + fileName));
    28         // 获取根节点
    29         Element root = document.getDocumentElement();
    30         System.out.println(root.getNodeName());
    31  
    32         //读取database节点NodeList接口提供对节点的有序集合的抽象
    33         NodeList nodeList = root.getElementsByTagName("database");
    34         for (int i = 0; i < nodeList.getLength(); i++) {
    35             // 获取一个节点
    36             Node node = nodeList.item(i);
    37             // 获取该节点所有属性
    38             NamedNodeMap attributes = node.getAttributes();
    39             for (int j = 0; j < attributes.getLength(); j++) {
    40                 Node attribute = attributes.item(j);
    41                 System.out.println(attribute.getNodeName() + ":"
    42                         + attribute.getNodeValue());
    43             }
    44             //获取所有子节点数据
    45             NodeList childNodes=node.getChildNodes();
    46             for (int j = 0; j < childNodes.getLength(); j++) {
    47                 Node childNode=childNodes.item(j);
    48                 System.out.println(childNode.getNodeName()+":"+childNode.getNodeValue());
    49             }
    50         }
    51     }
    52  
    53     public static void main(String[] args) throws Exception {
    54         new DomHandler().read("data-source.xml");
    55  
    56     }
    57 }

    2.SAX方式解析:

     1 package com.wzh.sax; 
     3 import org.xml.sax.Attributes;
     4 import org.xml.sax.SAXException;
     5 import org.xml.sax.helpers.DefaultHandler;
     7 //
     8 public class Saxhandler extends DefaultHandler {
     9  
    10     @Override
    11     public void startDocument() throws SAXException {
    12         System.out.println("开始解析XML文档...");
    13     }
    14  
    15     @Override
    16     public void endDocument() throws SAXException {
    17         System.out.println("结束解析XML文档...");
    18     }
    19  
    20     @Override
    21     public void startElement(String uri, String localName, String qName,
    22             Attributes attributes) throws SAXException {
    23         // TODO Auto-generated method stub
    24         super.startElement(uri, localName, qName, attributes);
    25         System.out.println("开始解析节点["+qName+"]...");
    26         System.out.println("共有["+attributes.getLength()+"]个属性");
    27     }
    28  
    29     @Override
    30     public void characters(char[] ch, int start, int length)
    31             throws SAXException {
    32         // TODO Auto-generated method stub
    33         super.characters(ch, start, length);
    34         String content =new String(ch,start,length);
    35         System.out.println(content);
    36     }
    37  
    38     @Override
    39     public void endElement(String uri, String localName, String qName)
    40             throws SAXException {
    41         // TODO Auto-generated method stub
    42         super.endElement(uri, localName, qName);
    43         System.out.println("结束解析XML节点...");
    44     }
    45 }

    3.DOM4J方式解析:

      1 package com.wzh.dom4j;
      3 import java.io.FileOutputStream;
      4 import java.sql.DatabaseMetaData;
      5 import java.util.Iterator;
      6 import java.util.List;
      8 import org.dom4j.*;
      9 import org.dom4j.io.OutputFormat;
     10 import org.dom4j.io.SAXReader;
     11 import org.dom4j.io.XMLWriter;
     12
     13 public class Dom4jHandler {
     15 public void add() throws Exception {
     16 // 1.创建一个Document
     17 Document document = DocumentHelper.createDocument();
     18 // 2.给Document添加数据
     19 Element root = document.addElement("DataSource");
     20 // 添加注释
     21 root.addComment("这是注释信息");
     22 // 在root根节点下面添加一个子节点
     23 Element database = root.addElement("database");
     24 database.addAttribute("name", "mysql");
     25 database.addAttribute("version", "5.0");
     26 // 添加子节点
     27 database.addElement("driver").setText("com.mysql.jdbc.Driver");
     28 database.addElement("url")
     29 .setText("jdbc:mysql://localhost:3306/myjdbc");
     30 database.addElement("user").setText("root");
     31 database.addElement("password").setText("root");
     32 // 3.将Document写出文件
     33 OutputFormat format = OutputFormat.createPrettyPrint();
     34 format.setEncoding("utf-8");
     35 // FileOutputStream默认生成的路径在根路径
     36 XMLWriter xw = new XMLWriter(new FileOutputStream("db.xml"), format);
     37 xw.write(document);
     38 xw.close();
     39 }
     40 
     41 public void update(String fileName) throws Exception {
     42 // sax解析器
     43 SAXReader saxReader = new SAXReader();
     44 // 读到对象
     45 Document document = saxReader.read(this.getClass().getResourceAsStream(
     46 "/" + fileName));
     47 Element root = document.getRootElement();
     48 List<Element> databases_node = root.elements("database");
     49 for (Element database_node : databases_node) {
     50 if (database_node.attributeValue("name").equalsIgnoreCase("mysql")) {
     51 System.out.println("old:"
     52 + database_node.attributeValue("name"));
     53 database_node.attribute("name").setText("Oracle");
     54 System.out.println("update:"
     55 + database_node.attributeValue("name"));
     56 
     57 database_node.element("driver").setText("oracel");
     58 database_node.element("url").setText("jdbc");
     59 
     60 // 删除password节点
     61 database_node.remove(database_node.element("password"));
     62 
     63 // 删除属性
     64 database_node.remove(database_node.attribute("version"));
     65 }
     66 }
     67 
     68 OutputFormat format = OutputFormat.createPrettyPrint();
     69 format.setEncoding("utf-8");
     70 // FileOutputStream默认生成的路径在根路径
     71 XMLWriter xw = new XMLWriter(new FileOutputStream("db2.xml"), format);
     72 xw.write(document);
     73 xw.close();
     74 }
     75 
     76 public void read(String fileName) throws Exception {
     77 // sax解析器
     78 SAXReader saxReader = new SAXReader();
     79 // 读到对象
     80 Document document = saxReader.read(this.getClass().getResourceAsStream(
     81 "/" + fileName));
     82 Element root = document.getRootElement();
     83 System.out.println("根节点:" + root.getName());
     84 
     85 // List<Element> childElements=root.elements();
     86 List<Element> childElements = root.elements("database");
     87 for (Element child : childElements) {
     88 // 获取属性 不知道属性名称时的遍历方法
     89 List<Attribute> attributes = child.attributes();
     90 // for (Attribute attribute : attributes) {
     91 // System.out.println(attribute.getName()+":"+attribute.getValue());
     92 // }
     93 String name = child.attributeValue("name");
     94 // String version = child.attributeValue("version");
     95 String version = child.attribute("version").getValue();
     96 System.out.println(name + ":" + version);
     97 
     98 // //获取子节点
     99 // List<Element> childs=child.elements();
    100 // for (Element element : childs) {
    101 // System.out.println(element.getName()+":"+element.getText());
    102 // }
    103 System.out.println(child.elementText("driver"));
    104 System.out.println(child.element("url").getText());
    105 System.out.println(child.elementTextTrim("user"));
    106 System.out.println(child.element("password").getTextTrim());
    107 
    108 }
    109 }
    110 
    111 public static void main(String[] args) throws Exception {
    112 // new Dom4jHandler().read("data-source.xml");
    113 // new Dom4jHandler().add();
    114 new Dom4jHandler().update("data-source.xml");
    115 }
    116 }

    4.总结:

    DOM:在解析文件之前需要将文档一次性加载到内存中,适合对文件的随机访问,不适合顺序访问。

    SAX:是基于事件驱动的解析方式,它顺序读取XML文件,当遇到文档开始,文档结束,标签开始,标签结束时都会触发响应的事件,用户通过在其回调事件中写入处理代码,适合对XML的顺序访问。

    DOM4j:dom4j是一个简单的开源库,用于处理XML、 XPath和XSLT,它基于Java平台,使用Java的集合框架,全面集成了DOM,SAX和JAXP,是目前比较流行XML文档解析方法。

  • 相关阅读:
    C:大数相加
    杭电2186:悼念512汶川大地震遇难同胞——一定要记住我爱你
    实验五
    安装vmtools
    ubuntu20.04换源
    实验一 灯程序——OK6410A开发板LINUX3.0.1(嵌入式开发)
    OK6410A开发板LINUX3.0.1配置(嵌入式开发)
    实验四 Makefile
    虚拟机联网
    实验三 按键灯
  • 原文地址:https://www.cnblogs.com/GISQZC/p/5934328.html
Copyright © 2011-2022 走看看