zoukankan      html  css  js  c++  java
  • XML案例(使用JAXP进行SAX解析)

    1.Book.java

    package cn.itcast.sax;

    public class Book {
    private String name;
    private String author;
    private String price;
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public String getAuthor() {
    return author;
    }
    public void setAuthor(String author) {
    this.author = author;
    }
    public String getPrice() {
    return price;
    }
    public void setPrice(String price) {
    this.price = price;
    }

    }

    2.Demo1.java

    package cn.itcast.sax;

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

    import org.junit.Test;
    import org.xml.sax.Attributes;
    import org.xml.sax.ContentHandler;
    import org.xml.sax.Locator;
    import org.xml.sax.SAXException;
    import org.xml.sax.XMLReader;

    public class Demo1 {
    /*
    * sax 解析xml文档
    */
    @Test
    public void test1() throws Exception, SAXException {

    // 1.创建解析工厂
    SAXParserFactory factory = SAXParserFactory.newInstance();

    // 2.得到解析器
    SAXParser sp = factory.newSAXParser();

    // 3.得到读取器
    XMLReader reader = sp.getXMLReader();

    // 4.设置内容处理器
    reader.setContentHandler(new ListHandler());

    // 5.读取xml文档内容
    reader.parse("src/book.xml");
    }

    }

    // 得到xml文档所有内容
    class ListHandler implements ContentHandler {
    @Override
    public void startElement(String uri, String localName, String qName,
    Attributes atts) throws SAXException {
    System.out.println("<" + qName + ">");

    for (int i = 0; atts != null & i < atts.getLength(); i++) {
    String attName = atts.getQName(i);
    String attValue = atts.getValue(i);
    System.out.println(attName + "=" + attValue);
    }
    }

    @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 {

    System.out.println(new String(ch, start, length));
    }

    @Override
    public void ignorableWhitespace(char[] ch, int start, int length)
    throws SAXException {
    // TODO Auto-generated method stub

    }

    @Override
    public void processingInstruction(String target, String data)
    throws SAXException {
    // TODO Auto-generated method stub

    }

    @Override
    public void skippedEntity(String name) throws SAXException {
    // TODO Auto-generated method stub

    }

    @Override
    public void startDocument() throws SAXException {
    // TODO Auto-generated method stub

    }

    @Override
    public void endDocument() throws SAXException {
    // TODO Auto-generated method stub

    }

    @Override
    public void startPrefixMapping(String prefix, String uri)
    throws SAXException {
    // TODO Auto-generated method stub

    }

    @Override
    public void endPrefixMapping(String prefix) throws SAXException {
    // TODO Auto-generated method stub

    }

    @Override
    public void setDocumentLocator(Locator locator) {
    // TODO Auto-generated method stub

    }

    }

    3.Demo2.java

    package cn.itcast.sax;

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

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

    public class Demo2 {
    /*
    * sax 解析xml文档
    */
    @Test
    public void test1() throws Exception, SAXException {

    // 1.创建解析工厂
    SAXParserFactory factory = SAXParserFactory.newInstance();

    // 2.得到解析器
    SAXParser sp = factory.newSAXParser();

    // 3.得到读取器
    XMLReader reader = sp.getXMLReader();

    // 4.设置内容处理器
    reader.setContentHandler(new TagValueHandler());

    // 5.读取xml文档内容
    reader.parse("src/book.xml");
    }

    // 获取指定标签的值
    class TagValueHandler extends DefaultHandler {

    private String currentTag;// 记住当前解析到的是什么标签
    private int needNumber=2;//记住想获取第几个作者标签的值
    private int currentNumber;//当前解析到的是第几个

    @Override
    public void startElement(String uri, String localName, String qName,
    Attributes attributes) throws SAXException {
    currentTag = qName;
    if ("作者".equals(currentTag))
    currentNumber++;
    }

    @Override
    public void endElement(String uri, String localName, String qName)
    throws SAXException {
    currentTag = null;
    }

    @Override
    public void characters(char[] ch, int start, int length)
    throws SAXException {
    if ("作者".equals(currentTag)&&currentNumber==needNumber)
    System.out.println(new String(ch, start, length));
    }

    }

    }

    4.Demo3.java

    package cn.itcast.sax;

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

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

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

    import cn.itcast.sax.Demo2.TagValueHandler;

    public class Demo3 {
    /*
    * sax 解析xml文档
    */
    @Test
    public void test1() throws Exception, SAXException {

    // 1.创建解析工厂
    SAXParserFactory factory = SAXParserFactory.newInstance();

    // 2.得到解析器
    SAXParser sp = factory.newSAXParser();

    // 3.得到读取器
    XMLReader reader = sp.getXMLReader();

    // 4.设置内容处理器
    BeanListHandler handler=new BeanListHandler();
    reader.setContentHandler(handler);

    // 5.读取xml文档内容
    reader.parse("src/book.xml");

    List<Book> list=handler.getBooks();
    for(Book book :list){
    System.out.println("书名:"+book.getName());
    System.out.println("作者:"+book.getAuthor());
    System.out.println("价格:"+book.getPrice());
    System.out.println();
    }
    }

    // 把xml文档中的每一本书封装到一个book对象,并把多个book对象放在一个list集合中返回
    class BeanListHandler extends DefaultHandler {

    private List list = new ArrayList();
    private String currentTag;
    private Book book;

    @Override
    public void startElement(String uri, String localName, String qName,
    Attributes attributes) throws SAXException {
    currentTag = qName;
    if ("书".equals(currentTag))
    book = new Book();
    }

    @Override
    public void endElement(String uri, String localName, String qName)
    throws SAXException {
    if ("书".equals(qName)) {
    list.add(book);
    book = null;
    }
    currentTag = null;

    }

    @Override
    public void characters(char[] ch, int start, int length)
    throws SAXException {
    if ("书名".equals(currentTag)) {
    String value = new String(ch, start, length);
    book.setName(value);
    }
    if ("作者".equals(currentTag)) {
    String value = new String(ch, start, length);
    book.setAuthor(value);
    }
    if ("售价".equals(currentTag)) {
    String value = new String(ch, start, length);
    book.setPrice(value);
    }
    }

    public List getBooks() {
    return list;
    }

    }
    }

  • 相关阅读:
    基于结构化平均感知机的分词器Java实现
    HanLP分词命名实体提取详解
    [英语学习]王秒同学《21天TED英语精练团》
    [不好分类]转帖:好好说话是个“技术活”(李笑来说话太直了?)
    [英语学习]3招速成英语发音 背景音乐和学习随感
    [读书笔记]《番茄工作法图解:简单易行的时间管理方法》
    [英语学习]给宝宝的英语原版资源
    [办公自动化]如何判断服务器是否开放某端口
    [他山之石]Google's Project Oxygen Pumps Fresh Air Into Management
    [读书笔记]云计算时代的网络,读《腾云,云计算和大数据时代网络技术揭秘》
  • 原文地址:https://www.cnblogs.com/xiaohuihui123/p/4359564.html
Copyright © 2011-2022 走看看