根据xml的标签解析文件的Demo

package xmlExtractor;

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

import java.util.ArrayList;
import java.util.List;

public class SAXHandler extends DefaultHandler {
    int bookIndex = 0;
    private List<Book> bookList = new ArrayList<Book>();
    private Book book = null;
    private String value = null;

    //用来标志解析开始
    @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 {


        if (qName.equals("book")) {
            bookIndex++;
            book = new Book();
            System.out.println("============================第" + bookIndex + "本书开始了=========================");
            //已知book下属性元素的名称
            //System.out.println(attributes.getValue("id"));
            for (int i = 0; i < attributes.getLength(); i++) {
                String name = attributes.getQName(i);
                if (name.equals("id")) {
                    book.setId(attributes.getValue(i));
                } else if (name.equals("class")) {
                    book.setClassName(attributes.getValue(i));
                }
                book.setId(attributes.getValue(i));
                //System.out.println(name);
                System.out.println("第" + bookIndex + "本书的" + attributes.getQName(i) + "是:" + attributes.getValue(i)/*attributes.getValue(Qname)*/);
            }
        } else if (!qName.equals("bookStore")) {
            System.out.print("第" + bookIndex + "本书的" + qName + "是:");
        }

    }

    //遍历标签内的内容
    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        value = new String(ch, start, length);
        //如果标签下没有内容,如bookstore与book之间就没有内容,将不打印
        if (!value.trim().equals("")) {
            System.out.println(value);

        }

    }

    //遍历元素的结束标签
    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (qName.equals("book")) {
            bookList.add(book);
            book = null;
            System.out.println("============================第" + bookIndex + "本书结束了=========================");
        } else if (qName.equals("name")) {
            book.setName(value);
        } else if (qName.equals("author")) {
            book.setAuthor(value);
        } else if (qName.equals("year")) {
            book.setYear(value);
        }

    }


    public List<Book> getBookList() {
        return bookList;
    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值