1. 程式人生 > >SAX解析文件

SAX解析文件

pan cto args closed exce sax解析 ++ 解析 efault

 1 import javax.xml.parsers.ParserConfigurationException;
 2 import javax.xml.parsers.SAXParser;
 3 import javax.xml.parsers.SAXParserFactory;
 4 /**
 5  * 1.新建一個SAX解析工廠實例
 6  * 2.new一個解析器
 7  * 3.new一個解析處理器
 8  * 4.解析文件
 9  * 5.獲取信息
10  * @author Administrator
11  *
12  */
13 public class SAXParserDemo {
14 15 public static void main(String[] args) { 16 SAXParserFactory factory = SAXParserFactory.newInstance();// 工廠模式,單例模式 17 try { 18 /* 獲得一個sax解析器 */ 19 SAXParser parser = factory.newSAXParser(); 20 /* 解析器解析這個文件,new一個解析處理器 */ 21 SAXParserHandler sax = new
SAXParserHandler(); 22 parser.parse("books.xml", sax); 23 System.out.println("幾本書:" + sax.booklist.size()); 24 25 for (Book book : sax.booklist) { 26 System.out.println(book.getId()); 27 System.out.println(book.getName());
28 System.out.println(book.getPrice()); 29 System.out.println(book.getAuthor()); 30 31 } 32 33 } catch (ParserConfigurationException e) { 34 35 e.printStackTrace(); 36 } catch (Exception e) { 37 38 e.printStackTrace(); 39 } 40 41 } 42 43 }
技術分享圖片
  1 package SAXParser1;
  2 
  3 import java.util.ArrayList;
  4 
  5 import org.xml.sax.Attributes;
  6 import org.xml.sax.SAXException;
  7 import org.xml.sax.helpers.DefaultHandler;
  8 
  9 /**
 10  * @author xuhua 解析處理器
 11  * 1.重寫方法
 12  */
 13 public class SAXParserHandler extends DefaultHandler {
 14     /* 全局變量 */
 15     Book book;
 16     String value;
 17     ArrayList<Book> booklist = new ArrayList<Book>();
 18 
 19     /**
 20      * 解析開始
 21      */
 22     @Override
 23     public void startDocument() throws SAXException {
 24         super.startDocument();
 25         System.out.println("parse start");
 26     }
 27 
 28     /**
 29      * 解析結束
 30      */
 31     @Override
 32     public void endDocument() throws SAXException {
 33         super.endDocument();
 34         System.out.println("parse end");
 35     }
 36 
 37     /**
 38      * 開始解析標簽
 39      * qName:標簽名字  Attributes:屬性
 40      */
 41     @Override
 42     public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
 43 
 44         super.startElement(uri, localName, qName, attributes);
 45 
 46         if (qName.equals("book")) {
 47             book = new Book();
 48             String id = attributes.getValue("id");//獲取id屬性值
 49             System.out.println(id);
 50 
 51             /* 獲取屬性名與屬性值 */
 52             int num = attributes.getLength(); // 屬性個數
 53             for (int i = 0; i < num; i++) {
 54                 System.out.println("屬性名:" + attributes.getQName(i));
 55                 System.out.println("屬性值:" + attributes.getValue(i));
 56                 /* 把id屬性值set進book實體中 */
 57                 if (attributes.getQName(i).equals("id")) {
 58                     book.setId(attributes.getValue(i));
 59                 }
 60             }
 61         }
 62 
 63         System.out.println("屬性名:" + qName);
 64     }
 65 
 66     /**
 67      * 結束標簽解析
 68      */
 69     @Override
 70     public void endElement(String uri, String localName, String qName) throws SAXException {
 71 
 72         super.endElement(uri, localName, qName);
 73         if (qName.equals("book")) {
 74             booklist.add(book);
 75             book = null;
 76             System.out.println("==============結束遍歷某一本書的內容=============");
 77         }
 78         /* id為book節點的屬性,其他為節點值 */
 79         // else if(qName.equals("id"))
 80         // {
 81         // book.setId(value);
 82         // }
 83         else if (qName.equals("name")) {
 84             book.setName(value);
 85         } else if (qName.equals("price")) {
 86             book.setPrice(value);
 87         } else if (qName.equals("author")) {
 88             book.setAuthor(value);
 89         }
 90         System.out.println(qName + "元素結束");
 91 
 92     }
 93 
 94     /**
 95      * 獲取節點值
 96      */
 97     @Override
 98     public void characters(char[] ch, int start, int length) throws SAXException {
 99 
100         super.characters(ch, start, length);
101         /**/
102         value = new String(ch, start, length);
103         // if(value.trim()!=null)
104         if (!value.trim().equals("")) // 標簽之間存在空格
105             System.out.println(value);
106     }
107 
108 }
解析處理器 技術分享圖片
 1 package SAXParser1;
 2 
 3 public class Book {
 4     private String id;
 5     private String name;
 6     private String price;
 7     private String author;
 8     public String getId() {
 9         return id;
10     }
11     public void setId(String id) {
12         this.id = id;
13     }
14     public String getName() {
15         return name;
16     }
17     public void setName(String name) {
18         this.name = name;
19     }
20     public String getPrice() {
21         return price;
22     }
23     public void setPrice(String price) {
24         this.price = price;
25     }
26     public String getAuthor() {
27         return author;
28     }
29     public void setAuthor(String author) {
30         this.author = author;
31     }
32     
33 }
book實體javabean 技術分享圖片
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <bookstore>
 3     <book id="1">
 4         <name>java入門</name>
 5         <price>80</price>
 6         <author>張思</author>
 7     </book>
 8     <book id="2">
 9         <name>java進階</name>
10         <price>90</price>
11         <author>葉東</author>
12     </book>
13 
14 </bookstore>
xml文件

SAX解析文件