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文件
时间: 2024-11-06 11:37:21