zoukankan      html  css  js  c++  java
  • 把XML文档中的每一本书封装到一个book对象,并把多个book对象放到一个list集合当中返回

     1 package sax;
     2 
     3 import java.awt.print.Book;
     4 import java.io.IOException;
     5 import java.util.ArrayList;
     6 import java.util.List;
     7 
     8 import javax.xml.parsers.ParserConfigurationException;
     9 import javax.xml.parsers.SAXParser;
    10 import javax.xml.parsers.SAXParserFactory;
    11 
    12 import org.xml.sax.Attributes;
    13 import org.xml.sax.SAXException;
    14 import org.xml.sax.XMLReader;
    15 import org.xml.sax.helpers.DefaultHandler;
    16 
    17 public class Demo {
    18 
    19     public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
    20         //1、创建解析工厂
    21         SAXParserFactory factory=SAXParserFactory.newInstance();
    22         //2、得到解析器
    23         SAXParser sp=factory.newSAXParser();
    24         //3、得到读取器
    25         XMLReader reader=sp.getXMLReader();
    26         //4、设置内容处理
    27         BeanListHandler handle=new BeanListHandler();
    28         reader.setContentHandler(handle);
    29         //5、读取xml文档内容
    30         reader.parse("src/sax/shu.xml");
    31         List<shu> list=handle.getshu();
    32         System.out.print(list);
    33         
    34     }
    35 
    36 }
    37 //把XML文档中的每一本书封装到一个book对象,并把多个book对象放到一个list集合当中返回
    38 class BeanListHandler extends DefaultHandler
    39 {
    40     private List list=new ArrayList();
    41     private String currentTag;
    42     private shu book;
    43     @Override
    44     public void startElement(String uri, String localName, String qName,
    45             Attributes attributes) throws SAXException {
    46         if("书".equals(currentTag))
    47         {
    48             book=new shu();
    49             
    50         }
    51     }
    52     @Override
    53     public void characters(char[] ch, int start, int length)
    54             throws SAXException {
    55         if("书名".equals(currentTag))
    56         {
    57             String name=new String(ch,start,length);
    58             book.setName(name);
    59         }
    60         if("作者".equals(currentTag))
    61         {
    62             String author=new String(ch,start,length);
    63             book.setAuthor(author);;
    64         }
    65         if("售价".equals(currentTag))
    66         {
    67             String price=new String(ch,start,length);
    68             book.setPrice(price);
    69         }
    70     }
    71 
    72     @Override
    73     public void endElement(String uri, String localName, String name)
    74             throws SAXException {
    75         currentTag=null;
    76         if("书".equals(name))
    77         {
    78             list.add(book);
    79             book=null;
    80         }
    81         
    82     }
    83     public List getshu() {
    84         return list;
    85     }
    86 
    87     
    88     
    89 }

    程序不完整,没有打印功能,只能用debug调试看结果

  • 相关阅读:
    网页图表控件Highcharts选项配置参数
    网页无法复制粘贴怎么办
    PHP #2003
    IE下有没有类似于Firebug的调试工具
    常用HTML标签的全称及描述
    [Angular] Improve Server Communication in Ngrx Effects with NX Data Persistence in Angular
    [Webpack] Detect Unused Code with Webpack and unused-files-webpack-plugin
    [Functional Programming Monad] Combine Stateful Computations Using Composition
    [Functional Programming Monad] Combine Stateful Computations Using A State Monad
    [Algorithm] Find Max Items and Max Height of a Completely Balanced Binary Tree
  • 原文地址:https://www.cnblogs.com/jjlovemm/p/4270484.html
Copyright © 2011-2022 走看看