SAX-解析XML

基于SAX的xml解析

sax通常用来进行查找,就是搜索使用,大概流程如下

  • 解析器工厂
  • 解析器
  • XMLReader
  • 处理器ContentHandler,这部分需要程序员自己写
    这里写图片描述

进行遍历和指定位置查找

package sax;

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

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;

public class Sax {

    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
        // 解析工厂
        SAXParserFactory factory = SAXParserFactory.newInstance();
        // 解析器
        SAXParser parser = factory.newSAXParser();
        // 解析器绑定reader
        XMLReader reader = parser.getXMLReader();
        // 设置处理
        reader.setContentHandler(new MyContentHandler2());
        // 进行解析
        reader.parse("src/test.xml");
    }

}

// 遍历所有的节点
class MyContentHandler extends DefaultHandler {

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        System.out.println("标签名称:" + qName);
        for (int i = 0; i < attributes.getLength(); i++) {
            String name = attributes.getQName(i);
            String value = attributes.getValue(i);

            System.out.println("属性名称为:" + name + "值为:" + value);
        }
        super.startElement(uri, localName, qName, attributes);
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        super.endElement(uri, localName, qName);
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        System.out.println("标签的值为" + new String(ch, start, length));
        super.characters(ch, start, length);
    }
}

//获取指定的节点的相关内容
class MyContentHandler2 extends DefaultHandler {

    // 标记目标元素,通过start end 对标记的修改来读标签内容
    private boolean falg = false;

    // 当有多个标记命中的时候,index代表第几个标签
    private int index = 0;

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if (qName.equals("书名") && index == 0) {
            for (int i = 0; i < attributes.getLength(); i++) {
                String name = attributes.getQName(i);
                String value = attributes.getValue(i);
                System.out.println("标签的属性为: " + name + "  对应的值为:" + value);
                falg = true;
            }
        }
        super.startElement(uri, localName, qName, attributes);
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        falg = false;
        index++;
        super.endElement(uri, localName, qName);
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if (falg) {
            System.out.println("标签内容为" + new String(ch, start, length));
        }
        super.characters(ch, start, length);
    }
}

小结

SAX的优势在于,找多少就加载多少,不会一下子挤满内存,但是缺点是比较麻烦- -,处理部分 得程序员自己手写,其实也还好.做个笔记把

猜你喜欢

转载自blog.csdn.net/qq_41376740/article/details/80984396