使用Sax和Pull方法 解析XML文件
2015-09-19 08:59
211 查看
xml 可扩展标签语言
结构良好的xml文档
1.标签成对出现
2.根元素有且只有一个
3.大小写敏感
4.不能嵌套和交叉
5.属性要用双引号或者单引号
6.包含xml声明
解析MXL文件 Sax解析的步骤
1.创建工厂 SaxParserFactory
2.创建解析器 AaxParser
3.创建读取器XmlReader
4.设置时间处理器 SetContentHandler();
5.解析 parse(“路径”)
额外: 创建事件处理器
例 : books.xml文件
<?xml version="1.0" encoding="UTF-8"?> <bookstore> <book category='java'> <title lang="english">java基础</title> <author>gosling</author> <year>2014</year> <price>200</price> </book> <book category="android"> <title lang="chinese">android基础</title> <author>google</author> <year>2014</year> <price>20</price> </book> </bookstore> 书籍类
public class Book { private int id; private String bookname; private String author; private double price; public Book() { // TODO Auto-generated constructor stub } public Book(int id, String bookname, String author, double price) { super(); this.id = id; this.bookname = bookname; this.author = author; this.price = price; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getBookname() { return bookname; } public void setBookname(String bookname) { this.bookname = bookname; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public void showInfo(){ System.out.println("id:"+id+" bookname:"+bookname+" author:"+author+" price:"+price); } }
用SAX解析 @需求分析: 要求:把 books.xml文件中的书籍信息封装成对象,然后放入集合中 Public class SAXMain { public static void main(String[]args) throws Exception{ //创建工厂 SAXParserFactory factory=SAXParserFactory.newInstance(); //2.创建解析器 SAXParser parser = factory.newSAXParser(); //3.获取xmlreader XMLReader reader = parser.getXMLReader(); //4.设置处理器 BookContentHandler handler=new BookContentHandler(); //5.解析 reader.parser("xml\\books.xml"); //6.遍历集合 } } //事件处理器 class BookContentHandler extends DefaultHandler{ ArraryList<Book> books = new ArrayList<Book>(); Book book=null; String currentTagName;//当前读到的开始标签 public void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException { //把当前读到的标签设置到currentTagName currentTagName = qName; if(qName.equals("book")) { book = new Bok(); //获取id String id=attributes.getValue("id"); book.setID(Integer.parseInt(id)); } public void endElement(String uri, String localName, String qName)throws SAXException { currentTagName="";//跳过空格和换行 if(qName.equals("book")) { books.add(book);//添加到集合 book=null; } } public void characters(char[] ch, int start, int length) throws SAXException { //判断bookname if(currentTagName.equals("bookname")) { book.setBookname(new String(ch,start,length)); }else if(currentTagName.equals("author")) { book.setAuthor(new String(ch,start,length)); }else if(currentTagName.equals("price")) { double price=Double.parserDouble(new String(ch,start,length)) book.setPrice(price); } } } } PULl解析 1 创建工厂 XmlPullParserFactory 2 创建解析器 XmlPullParser 3 设置读取的文件流 4 获取第一个事件类型 5 while循环读取 type=parser.next(); 下一个类型 parser.getName() 获取标签名称 parser.nextText(); 获取标签中的内容 @需求分析: 使用PULL方式解析books.xml ,封装成书对象,然后放入集合 public class PullMain{ public static void main(String[]args) throws Exception{ //1.创建工厂 XmlPullParserFactory factory=XmlPullParserFactory .newInstance(); //2.创建解析器 XmlPullParser parser = factory.newPullParser(); //3.设置要读取的xml文件,并指定编码 parer.setInput(new FileInputStream("xml\\books.xml"),"gbk"); //4.获取第一个事件类型 int type=parser.getEventType(); ArrayList<Book>books=new ArraryList<book>(); Book book=null; while(type!=XmlPullParser.END_DOCUMENT) { switch(type) { case XmlPullParser.STATR_TAG: //getName()获取标签名称 if(parser.getName().equals("book")) { book = new Book(); //获取id String id=parser.getAttributeValue(null,"id"); book.setId(Integer.parserInt(id)); }else if(parser.getName().equals("bookname")) { book.setBookname(parser.nextText()); }else if(parser.getName().equals("author")) { book.setAuthor(parser.nextText()); }else if (parser.getName().equals("price")) { String price = parser.nextText(); book.setPrice(Double.parseDouble(price)); } break; case XmlPullParser.END_TAG:// 3 //当读取book结束标记的时候 ,添加到集合中 if (parser.getName().equals("book")) { books.add(book); } break; } //获取下一个事件 type = parser.next(); } for (Book b : books) { b.showInfo(); } } }阅读更多
相关文章推荐
- java中解析xml文件的五种常见方法:DOM4J,dom,pull,SAX,Jdom
- Java乔晓松-使用SAX,DOM,pull解析XML文件以及pull解析生成xml文件
- Android Studio ——Android 使用Pull方法解析XML文件的方法
- Android程序解析XML文件的方法及使用PULL解析XML案例
- 安卓使用DOM,SAX,PULL解析xml,安卓写入xml文件
- Android三种解析xml文件的方法:SAX, PULL, DOM
- android学习——使用SAX、DOM 和 PULL 解析xml文件,及使用pull生成xml文件
- 使用SAX或者DOM或者pull解析XML文件的使用
- Android学习笔记_7_使用 sax 或者 dom 或者 pull 解析XML文件
- 使用SAX或者DOM或者pull解析XML文件
- Android 使用Pull方法解析XML文件的方法
- android学习——使用SAX、DOM 和 PULL 解析xml文件,及使用pull生成xml文件
- Android使用pull方法解析XML文件
- Pull解析XML文件,使用HttpClient方法
- Android程序解析XML文件的方法及使用PULL解析XML案例
- 分别使用sax、dom、pull对下述的xml文件进行解析
- 使用pull方法解析Xml文件(省市县Xml)
- Android演示使用PULL方式解析XML文件
- android XML文件解析之 SAX解析方法
- Android实战技巧:用Pull方法解析XML文件