XML文件解析DOM解析和SAX解析

解析一个XML文档有哪些内容
解析有:dom和sax两种
dom:把整个XML文档放入内存,适合XML随机访问,占用内存资源大
sax:事件驱动型的XML解析方式,顺序读取,不用一次装载整个文件,遇到标签会触发一个事件,适合对XML的顺序访问,占用内存资源稍小

Node:
  XML 文档的 documentElement 属性是根节点。
  nodeName 属性是节点的名称。nodeName 是只读的
  元素节点的 nodeName 与标签名相同
  属性节点的 nodeName 是属性的名称
  文本节点的 nodeName 永远是 #text
  文档节点的 nodeName 永远是 #document

  nodeType 属性是节点的类型。
  元素类型 节点类型
    元素 1
    属性 2
    文本 3
    注释 8
    文档 9

nodeValue属性规定节点的值。
元素节点的 nodeValue 是 undefined
文本节点的 nodeValue 是文本自身
属性节点的 nodeValue 是属性的值

DOM解析:

package com.briup.test3;


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
//用递归DOM方法解析
public class DomBookTest {
    //获取解析器 将其封装
    public static Document getDoc(String filename) throws Exception{
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        return builder.parse(filename);
    }
    
    public static void main(String[] args) throws Exception {
        //获取解析地址
        Document doc = getDoc("src/com/briup/test3/book.xml");
        //输出头部
        System.out.println("<? version="+doc.getXmlVersion()+" encoding="+doc.getXmlEncoding()+">");
        //获取根节点
        Element element = doc.getDocumentElement();
        printdoc(element);
    
    }
    public static void  printdoc(Node n){
        //获取节点类型
        short type = n.getNodeType();
        if (type==1) {
        //获取节点内容
            System.out.print("<"+n.getNodeName()+" ");
        //用Map集合存储获取的节点属性
            NamedNodeMap map = n.getAttributes();
            for (int i = 0; i < map.getLength(); i++) {
                Node attr =map.item(i);
                System.out.print(attr.getNodeName()+"="+attr.getNodeValue());
            }
            System.out.print(">");
        //获取孩子节点
            NodeList list = n.getChildNodes();
            for (int i = 0; i < list.getLength(); i++) {
            Node child = list.item(i);
            printdoc(child);
            }
            System.out.print("</"+n.getNodeName()+">");
            
        }    //获取节点内容
            else if(type ==3){
            System.err.print(n.getNodeValue());
            
        }
        
    }

}

SAX解析:

  

package com.briup.test3;



import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;

import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;



public class SaxBookTest {

    public static void main(String[] args) throws Exception{
        //获取解析器
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser parser = factory.newSAXParser();
        //使用内部类解析文件
        parser.parse("src/com/briup/test3/book.xml", new DefaultHandler(){

            
            //解析开始标题文档
            public void startDocument() throws SAXException {
                System.out.println("<?xml version= 1.0 encoding= utf-8 ?>");
                
            }

            //解析节点
            @Override
            public void startElement(String uri, String localName,
                    String qName, Attributes attributes) throws SAXException {
                System.out.print("<"+qName+" ");
                for (int i = 0; i < attributes.getLength(); i++) {
                    System.out.print(attributes.getQName(i)+"="+attributes.getValue(i));
                    
                }
                System.out.print(">");
            }

        
            @Override
            //解析结束
            public void endElement(String uri, String localName, String qName)
                    throws SAXException {
                System.out.print("</"+qName+">");
            }

            @Override
            //解析内容
            public void characters(char[] ch, int start, int length)
                    throws SAXException {
                String string = new String(ch, start, length);
                System.out.print(string);
            }
            
            
            
        });
    }

}

BOOK的XML文档

<?xml version="1.0" encoding="utf-8"?>
<books>
    <book bid="1">
        <name>java与模式</name>
        <price>80</price>
    </book>
    <book bid="2">
        <name>java编程思想</name>
        <price>95</price>
    </book>
    <book bid="3">
        <name>疯狂java讲义</name>
        <price>90</price>
    </book>
</books>
原文地址:https://www.cnblogs.com/yzqm666/p/5851175.html