xml解析-sax范例

前端之家收集整理的这篇文章主要介绍了xml解析-sax范例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

sax方式解析xml 文档跟dom最大的区别是,sax是基于读取到xml文档不同节点的产生不同的事件,然后回调Saxparse处理器里面的不同方法对节点进行不同处理。所以sax是基于事件触发机制的解析方式。
dom解析呢是根据xml文档建立dom节点树,然后对树里面的节点遍历从而对xml文档解析的

  1. package sax;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import org.xml.sax.Attributes;
  7. import org.xml.sax.SAXException;
  8. import org.xml.sax.helpers.DefaultHandler;
  9.  
  10. //定义一个自定义sax处理器,用于读取到element节点时回调其中的方法
  11. ublic class Saxparse extends DefaultHandler {
  12.  
  13. // 定义一个存放元素的list
  14. private List<Book> data;
  15. // 用于缓存当前标签名称
  16. private String elementName;
  17.  
  18. public List<Book> getData() {
  19. return data;
  20.  
  21. }
  22.  
  23. /** * 当遇到节点前半部分时候,如<price>触发这个方法 * * /** * xml如果没有约束 uri # null localname #null qName # 元素名称 * xml 约束 uri # * 命名空间值 targatNamespace="xxxxx" localname # 元素名称 * <table> * qName # 前缀:名称 <my:table> * */
  24. @Override
  25. public void startElement(String uri,String localName,String qName,Attributes attributes) throws SAXException {
  26. // TODO Auto-generated method stub
  27.  
  28. if ("book".equals(qName)) {
  29. // 说明解析到了一本新书
  30. Book book = new Book();
  31. data.add(book);// 添加到list中
  32. book.setId(attributes.getValue("id")); // 获取book的属性
  33. // 缓存当前标签名称
  34.  
  35. }
  36. this.elementName = qName;
  37. }
  38.  
  39. /** * 遇到如下字符串触发这个函数 <?xml version="1.0" encoding="UTF-8"?> * * @throws SAXException */
  40. @Override
  41. public void startDocument() throws SAXException {
  42. // TODO Auto-generated method stub
  43. super.startDocument();
  44. System.out.println("解析文档开始");
  45. data = new ArrayList<>();
  46. }
  47.  
  48. /** * 遇到一个节点的后半部,如</price> * * @param uri * @param localName * @param qName * @throws SAXException */
  49. @Override
  50. public void endElement(String uri,String qName) throws SAXException {
  51. // TODO Auto-generated method stub
  52. super.endElement(uri,localName,qName);
  53. System.out.println("元素结束" + "\tqName=" + qName + "\t localName=" + null);
  54. }
  55.  
  56. @Override
  57. public void endDocument() throws SAXException {
  58. // TODO Auto-generated method stub
  59. super.endDocument();
  60. System.out.println("文档结束");
  61. }
  62.  
  63. /** * 取每个节点下的文本串,如下面的100 <price> 100 </price> * * @param ch * @param start * @param length * @throws org.xml.sax.SAXException */
  64. @Override
  65. public void characters(char[] ch,int start,int length) throws org.xml.sax.SAXException {
  66. // TODO Auto-generated method stub
  67. super.characters(ch,start,length);
  68. String value = new String(ch,length);
  69. if ("title".equals(elementName)) {
  70. // value 表示title的数据
  71. data.get(data.size() - 1).setTitle(value);
  72.  
  73. }
  74. if ("price".equals(elementName)) {
  75. // value 表示price的数据
  76. data.get(data.size() - 1).setPrice(value);
  77.  
  78. }
  79. if ("author".equals(elementName)) {
  80. data.get(data.size() - 1).setAuthor(value);
  81.  
  82. }
  83. this.elementName = null;
  84. }
  85.  
  86. public Saxparse() {
  87. // TODO Auto-generated constructor stub
  88. }
  89.  
  90. }

定义一个javabean,和xml一个book节点是一致的

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

用于解析的xml文档

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <books>
  3. <book id="b001">
  4. <title>android</title>
  5. <price>28</price>
  6. <author>vincent</author>
  7. </book>
  8. <book id="b002">
  9. <title>Vincent</title>
  10. <price>Vincent</price>
  11. <author>Vincent</author>
  12. </book>
  13. <book id="b0003">
  14. <title>Flex开发</title>
  15. <price>98</price>
  16. <author>胡玉勉</author>
  17.  
  18. </book>
  19.  
  20. <book id="b0003">
  21. <title>Flex开发</title>
  22. <price>98</price>
  23. <author>胡玉勉</author>
  24. </book>
  25. <book id="b0003">
  26.  
  27.  
  28.  
  29. <title>Flex开发</title>
  30. <price>98</price>
  31. <author>胡玉勉</author>
  32.  
  33. </book>
  34. <book id="b0003">
  35. <title>Flex开发</title>
  36. <price>98</price>
  37. <author>胡玉勉</author>
  38.  
  39. </book>
  40.  
  41. </books>

测试例程

  1. package sax.testrun;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.util.List;
  6.  
  7. import javax.xml.parsers.ParserConfigurationException;
  8. import javax.xml.parsers.SAXParser;
  9. import javax.xml.parsers.SAXParserFactory;
  10.  
  11. import org.xml.sax.SAXException;
  12.  
  13. import sax.Book;
  14. import sax.Saxparse;
  15.  
  16. public class RunSax {
  17.  
  18. public static void main(String[] args) throws ParserConfigurationException,SAXException {
  19. demo1();
  20. }
  21.  
  22. private static void demo1() throws ParserConfigurationException,SAXException {
  23. // 1、工厂
  24. SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
  25. // 2、解析器
  26. SAXParser saxParser = saxParserFactory.newSAXParser();
  27. Saxparse handler = new Saxparse();
  28. try {
  29. saxParser.parse(new File("src/data.xml"),handler);
  30. List<Book> books = handler.getData();
  31. for (Book book : books) {
  32. System.out.println(book);
  33.  
  34. }
  35. } catch (IOException e) {
  36. // TODO Auto-generated catch block
  37. e.printStackTrace();
  38. }
  39. ;
  40. }
  41. }

输出结果

解析文档开始 元素结束 qName=title localName=null 元素结束 qName=price localName=null 元素结束 qName=author localName=null 元素结束 qName=book localName=null 元素结束 qName=title localName=null 元素结束 qName=price localName=null 元素结束 qName=author localName=null 元素结束 qName=book localName=null 元素结束 qName=title localName=null 元素结束 qName=price localName=null 元素结束 qName=author localName=null 元素结束 qName=book localName=null 元素结束 qName=title localName=null 元素结束 qName=price localName=null 元素结束 qName=author localName=null 元素结束 qName=book localName=null 元素结束 qName=title localName=null 元素结束 qName=price localName=null 元素结束 qName=author localName=null 元素结束 qName=book localName=null 元素结束 qName=title localName=null 元素结束 qName=price localName=null 元素结束 qName=author localName=null 元素结束 qName=book localName=null 元素结束 qName=books localName=null 文档结束 Book [id=b001,title=android,price=28,author=vincent] Book [id=b002,title=Vincent,price=Vincent,author=Vincent] Book [id=b0003,title=Flex开发,price=98,author=胡玉勉] Book [id=b0003,author=胡玉勉]

猜你在找的XML相关文章