1 packagestudy;2 3 importjava.io.StringReader;4 5 importorg.kxml2.io.KXmlParser;6 importorg.xmlpull.v1.XmlPullParser;7 8 /**9 * 2008-4-16下午02:29:4010 *11 *@authorzhanghongzhi
12 * email:lanfanss@126.com
13 * desc:用来学习使用kxml来解析xml
14 * 书上讲解说,kxml是基于事件模型,按着下边的逻辑来读取标记,start_document-start_tag-text-end_tag-end_document
15 *16 */17 18 publicclassReadXmlByKXML {19 20 publicstaticvoidmain(String[] args)throwsException {21 XmlPullParser parser=newKXmlParser();22 String sample="\n"23 +"\n"24 +"It is the Sun!\n"25 +"It is a ball!\n"+"\n"26 +"I do not know!\n"+"\n";27 System.out.println("-----------待分析的内容------------");28 System.out.println(sample);29 System.out.println("---------------------------------");30 parser.setInput(newStringReader(sample));31 //获取事件类型32 inteventType=parser.getEventType();33 //如果不是整个文档的结尾,那么就读取,这样子就达到遍历整个文档的目的34 while(eventType!=XmlPullParser.END_DOCUMENT) {35 //访问文档根节点36 if(eventType==XmlPullParser.START_DOCUMENT) {37 System.out.println("当前节点的名称:"+parser.getName());38 System.out.println("当前节点下的属性:");39 intattrCount=parser.getAttributeCount();40 for(inti=0; i