zoukankan      html  css  js  c++  java
  • 每日日报

    sax

    原理 一个元素一个元素解析 解析到一个元素调用相关的方法 startDocument() 文档开始 endDocument 文档结束 startElement 元素开始 endElement元素结束 characters 文本内容

    @Test
    
    public void saxParser() throws Exception{
    
    //获取工厂
    
    SAXParserFactory parserFactory = SAXParserFactory.newInstance();
    
    SAXParser saxParser = parserFactory.newSAXParser();
    
    //获取xmlReader通过这个reader可以试着 ContentHandler
    
    XMLReader xmlReader = saxParser.getXMLReader();
    
    //给xmlReader设置contentHandler contentHandler是一个接口 里面太多的方法没实现
    
    //不去直接实现contenthandler 而是继承它默认的实现 DefaultHandler
    
    xmlReader.setContentHandler(new Myhandler());
    
    //解析xml文档
    
    xmlReader.parse("book.xml");
    
    }
    
    private class Myhandler extends DefaultHandler{
    
    @Override
    
    public void startDocument() throws SAXException {
    
    System.out.println("文档开始");
    
    }
    
    @Override
    
    public void endDocument() throws SAXException {
    
    System.out.println("文档结束");
    
    }
    
    @Override
    
    public void startElement(String uri, String localName, String qName, Attributes attributes)
    
    throws SAXException {
    
    System.out.println("开始标签<"+qName+">");
    
    }
    
    @Override
    
    public void endElement(String uri, String localName, String qName) throws SAXException {
    
    System.out.println("结束标签</"+qName+">");
    
    }
    
    @Override
    
    public void characters(char[] ch, int start, int length) throws SAXException {
    
    //文本内容
    
    String text = new String(ch,start,length);
    
    System.out.println("文本内容"+text);
    
    }
    
    }

    读出某一个元素对应的内容

    @Test
    
    public void saxParserTest2() throws Exception{
    
    SAXParserFactory parserFactory = SAXParserFactory.newInstance();
    
    SAXParser parser = parserFactory.newSAXParser();
    
    XMLReader reader = parser.getXMLReader();
    
    reader.setContentHandler(new DefaultHandler(){
    
    boolean isPrice = false;
    
    int count = 0;
    
    @Override
    
    public void startElement(String uri, String localName, String qName, Attributes attributes)
    
    throws SAXException {
    
    if("售价".equals(qName)){
    
    isPrice = true;
    
    count++;
    
    }
    
    }
    
    @Override
    
    public void endElement(String uri, String localName, String qName) throws SAXException {
    
    if("售价".equals(qName)){
    
    isPrice = false;
    
    }
    
    }
    
    @Override
    
    public void characters(char[] ch, int start, int length) throws SAXException {
    
    if(isPrice && count == 2){
    
    System.out.println(new String(ch,start,length));
    
    }
    
    }
    
    });
    
    reader.parse("book.xml");
    
    }

    解析成arraylist
    @Test
    
    public void saxParse2list() throws Exception{
    
    SAXParserFactory parserFactory = SAXParserFactory.newInstance();
    
    SAXParser parser = parserFactory.newSAXParser();
    
    XMLReader reader = parser.getXMLReader();
    
    reader.setContentHandler(new DefaultHandler(){
    
    ArrayList<Book> bookshelf = null;
    
    Book book = null;
    
    String tag = null;
    
    @Override
    
    public void startElement(String uri, String localName, String qName, Attributes attributes)
    
    throws SAXException {
    
    if("书架".equals(qName)){
    
    //创建集合
    
    bookshelf = new ArrayList<Book>();
    
    }else if("书".equals(qName)){
    
    //创建book对象
    
    book = new Book();
    
    }else if("书名".equals(qName)){
    
    tag = "书名";
    
    }else if("作者".equals(qName)){
    
    tag = "作者";
    
    }else if("售价".equals(qName)){
    
    tag = "售价";
    
    }
    
    }
    
    @Override
    
    public void endElement(String uri, String localName, String qName) throws SAXException {
    
    //每次走到结束标签 都要把tag复原为null
    
    tag = null;
    
    if("书架".equals(qName)){
    
    for(Book book1:bookshelf){
    
    System.out.println(book1);
    
    }
    
    }else if("书".equals(qName)){
    
    //读到书的结束标签 把book对象添加到集合中
    
    bookshelf.add(book);
    
    }
    
    }
    
    @Override
    
    public void characters(char[] ch, int start, int length) throws SAXException {
    
    if("书名".equals(tag)){
    
    book.setTitle(new String(ch,start,length));
    
    }else if("作者".equals(tag)){
    
    book.setAuthor(new String(ch,start,length));
    
    }else if("售价".equals(tag)){
    
    book.setPrice(new String(ch,start,length));
    
    }
    
    }
    
    });
    
    reader.parse("book.xml");
    
    }
  • 相关阅读:
    014.Nginx跨域配置
    013.Nginx动静分离
    附002.Nginx代理相关模块解析
    附001.Nginx location语法规则
    016.Nginx HTTPS
    015.Nginx重定向
    Blazor带我重玩前端(四)
    数据结构中的树(二叉树、二叉搜索树、AVL树)
    Flask前后端分离项目案例
    LeetCode-位运算相关题解
  • 原文地址:https://www.cnblogs.com/zhukaile/p/14833164.html
Copyright © 2011-2022 走看看