• SAX解析xml


    1.创建事件处理程序(编写ContentHandler的实现类,一般集成自DefaultHandler类, 采用 adapter 模式)
    2.创建SAX解析器
    3.将事件处理程序分配到解析器
    4.对文档进行解析,将每个事件发送给事件处理程序

    XMl文档

     1 <?xml version = "1.0" encoding = "UTF-8"?>
     2 <books>
     3     <book bookno="001">
     4         <title> java 编程</title>
     5         <author> me </author>
     6         <price> 80 </price>
     7     </book>
     8     <book bookno="002">
     9         <title> android 编程</title>
    10         <author> me </author>
    11         <price> 70 </price>
    12     </book>
    13 </books>

    Book基类

     1 public class Book {
     2     private String no;
     3     private String title;
     4     private String author;
     5     private Double price;
     6 
     7     public String getNo() {
     8         return no;
     9     }
    10 
    11     public void setNo(String no) {
    12         this.no = no;
    13     }
    14 
    15     public String getTitle() {
    16         return title;
    17     }
    18 
    19     public void setTitle(String title) {
    20         this.title = title;
    21     }
    22 
    23     public String getAuthor() {
    24         return author;
    25     }
    26 
    27     public void setAuthor(String author) {
    28         this.author = author;
    29     }
    30 
    31     public Double getPrice() {
    32         return price;
    33     }
    34 
    35     public void setPrice(double d) {
    36         this.price = d;
    37     }
    38 
    39     @Override
    40     public String toString() {
    41         return "book [no=" + no + ", title=" + title + ", author=" + author + ", price=" + price + "]";
    42     }
    43 
    44     public Book() {
    45         super();
    46         // TODO Auto-generated constructor stub
    47     }
    48 
    49     public Book(String no, String title, String author, Double price) {
    50         super();
    51         this.no = no;
    52         this.title = title;
    53         this.author = author;
    54         this.price = price;
    55     }
    56 
    57 }

    XMLParser接口

    1 import java.util.List;
    2 
    3 public interface XMLParser {
    4 public List<Book> parseXML(String filename);
    5 
    6 }

    SaxHandler处理程序

     1 /*
     2  * 
     3  * 提供事件回调方法
     4  * */
     5 import java.util.ArrayList;
     6 import java.util.List;
     7 
     8 import org.xml.sax.Attributes;
     9 import org.xml.sax.SAXException;
    10 import org.xml.sax.helpers.DefaultHandler;
    11 
    12 public class MySaxHandler extends DefaultHandler {
    13     private String tag;
    14     private Book book;
    15     private List<Book> list;
    16 
    17     public List<Book> getList() {
    18         return list;
    19     }
    20 
    21     @Override
    22     public void characters(char[] ch, int start, int length) throws SAXException {
    23         super.characters(ch, start, length);
    24         if (tag != null) {
    25             String str = new String(ch, start, length).trim();
    26             if (tag.equals("title")) {
    27                 book.setTitle(str);
    28             }
    29             if (tag.equals("author")) {
    30                 book.setAuthor(str);
    31             }
    32             if (tag.equals("price")) {
    33                 book.setPrice(Double.parseDouble(str));
    34             }
    35 
    36         }
    37     }
    38 
    39     @Override
    40     public void endDocument() throws SAXException {
    41         super.endDocument();
    42     }
    43 
    44     @Override
    45     public void endElement(String uri, String localName, String qName) throws SAXException {
    46         super.endElement(uri, localName, qName);
    47         if (qName.equals("book")) {
    48             list.add(book);
    49             book = null;
    50         }
    51         tag = null;
    52     }
    53 
    54     @Override
    55     public void startDocument() throws SAXException {
    56         super.startDocument();
    57         list = new ArrayList<Book>();
    58     }
    59 
    60     @Override
    61     public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    62 
    63         super.startElement(uri, localName, qName, attributes);
    64         if (qName.equals("book")) {
    65             book = new Book();
    66             String no = attributes.getValue("bookno");
    67             book.setNo(no);
    68 
    69         }
    70         tag = qName;
    71     }
    72 
    73 }

    SaxParser 解析器

    
    

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.List;

    
    

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

    
    

    import org.xml.sax.SAXException;

    
    

    public class SaxParser implements XMLParser {

    
    

    @Override
    public List<Book> parseXML(String filename) {
    List<Book> list = new ArrayList<Book>();
    MySaxHandler handler = null;
    //XML解析器
    SAXParserFactory factory = SAXParserFactory.newInstance();
    try {
    // xml解析器
    SAXParser parser = factory.newSAXParser();
    //文件输入流
    InputStream is = new FileInputStream(filename);
    //处理程序
    handler = new MySaxHandler();
    //分配处理程序到解析器
    parser.parse(is, handler);
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (ParserConfigurationException e) {
    e.printStackTrace();
    } catch (SAXException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    //提取在处理程序中得到的容器
    list = handler.getList();
    return list;
    }

    
    

    public static void main(String[] args) {
    XMLParser parser = new SaxParser();
    List<Book> list = parser.parseXML("src\book.xml");
    System.out.println("SAX解析结果:");
    for (Book book : list) {
    System.out.println(book.toString());
    }

    
    

    }

    
    

    }

    
    
  • 相关阅读:
    php 0916
    心里话
    LibreOJ 6559 小奇采药
    计蒜客 T1658 热浪
    计蒜客 T2021 飞扬的小鸟
    POJ 1276 Cash Machine
    SCU 3366 Watering Hole
    POJ 3268 Silver Cow Party
    luogu P4568 [JLOI2011]飞行路线
    POJ 1179 Polygon
  • 原文地址:https://www.cnblogs.com/the-wang/p/7119692.html
Copyright © 2020-2023  润新知