Pull解析XML
Pull解析技术与Sax解析技术原理相同,但比Sax解析简单,它们的解析速度和占用的资源差不多。Android内部使用的XML解析技术正是Pull,Android官方推荐开发者们使用Pull解析技术。Pull解析技术是第三方开发的开源技术,它同样可以应用于JavaSE开发。下面我们使用Pull解析技术解析XML文件,然后再使用Pull技术生成XML文件。 Pull解析XML文档 1.XML文件 依然使用上面的books.xml 2.XML文档对应的实体Book 依然使用上面的Book.java 3.Pull解析XML类 packagecom.changcheng.androidxml.xml; importjava.io.InputStream; importjava.io.Writer; importjava.util.ArrayList; importjava.util.List; importorg.xmlpull.v1.XmlPullParser; importorg.xmlpull.v1.XmlPullParserFactory; importorg.xmlpull.v1.XmlSerializer; importandroid.util.Xml; importcom.changcheng.androidxml.entity.Book; public classAndroidPullXML { public staticList readXML(InputStream inputStream, String inputEncoding)throwsException { // 创建Pull解析 XmlPullParserFactory pullParserFactory = XmlPullParserFactory .newInstance(); XmlPullParser pullParser = pullParserFactory.newPullParser(); // 解析XML pullParser.setInput(inputStream,inputEncoding); // 开始 inteventType = pullParser.getEventType(); List books =null; Book book =null; while(eventType != XmlPullParser.END_DOCUMENT) { String nodeName = pullParser.getName(); switch(eventType) { // 文档开始 caseXmlPullParser.START_DOCUMENT: books =newArrayList(); break; // 节点开始 caseXmlPullParser.START_TAG: if("book".equals(nodeName)) { book =newBook(); book.setId(Integer .parseInt(pullParser.getAttributeValue(0))); }else if("name".equals(nodeName)) { book.setName(pullParser.nextText()); }else if("price".equals(nodeName)) { book.setPrice(Float.parseFloat(pullParser.nextText())); } break; // 节点结束 caseXmlPullParser.END_TAG: if("book".equals(nodeName)) { books.add(book); book =null; } break; } eventType = pullParser.next(); } returnbooks; } } 4.编写测试Pull解析XML类 在sax测试类TestAndroidXML中添加一个测试方法: /** * 测试Pull解析XML *@throwsThrowable */ public voidtestAndroidPullReadXML()throwsThrowable { InputStream file =this.getClass().getClassLoader().getResourceAsStream("books.xml"); try{ List books = AndroidPullXML.readXML(file,"UTF-8"); Log.i(TAG,books.toString()); }catch(Exception e) { Log.e(TAG,e.toString()); } } 5.运行测试 在outline面板中的testAndroidPullReadXML方法或在TestAndroidXML类的testAndroidPullReadXML方法上右键->Debug As->Android Junit Test。运行结束后在LogCat面板中查看运行结束。 Pull生成XML文档 使用Pull生成上面的books.xml文档。 1.在AndroidPullXML类中添加一个方法: public static voidwriteXML(Writer writer,List books) throwsException { // 创建XML生成器 XmlSerializer writexml = Xml.newSerializer(); writexml.setOutput(writer); // 生成XML文档 writexml.startDocument("UTF-8",true); writexml.startTag("","books"); for(Book book : books) { // name writexml.startTag("","name"); writexml.attribute("","id",book.getId() + ""); writexml.text(book.getName()); writexml.endTag("","name"); // price writexml.startTag("","price"); writexml.text(book.getPrice() + ""); writexml.endTag("","price"); } // writexml.endTag("","books"); } 2.编写测试Pull生成XML方法 在sax测试类TestAndroidXML中添加一个测试方法: /** * 测试Pull生成XML *@throwsThrowable */ public voidtestAndroidPullWriteXML()throwsThrowable { // 生成到内存中。(也可以生成到文件中,那就需要定义一个文件输出流。) StringWriter writer =newStringWriter(); // 添加三本书 List books =newArrayList(); books.add(newBook(1,"C",89)); books.add(newBook(1,"C++",100)); books.add(newBook(1,"Java",87)); books.add(newBook(1,"JavaEE",95)); // 生成XML AndroidPullXML.writeXML(writer,books); // 打印结果 Log.i(TAG,books.toString()); } 3.运行测试 在outline面板中的testAndroidPullWriteXML方法或在TestAndroidXML类的testAndroidPullWriteXML方法上右键->Debug As->Android Junit Test。运行结束后在LogCat面板中查看运行结束。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |