解析books.xml文件,分别采用pull和sax的方式,怎么写呢?
book.xml 内容如下
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book>
<name>3天精通java</name>
<author>小德</author>
<price>88</price>
</book>
<book>
<name>1天精通android</name>
<author>小德</author>
<price>888</price>
</book>
</books>
谁能帮我写一下呢?
求代码 感觉真的好难!!
直接反序列化就简单。
额
//pull解析 public void parseXmlWithPull(String xmlData){ try{ XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); XmlPullParser xmlPullParser = factory.newPullParser(); xmlPullParser.setInput(new StringReader(xmlData)); int eventType = xmlPullParser.getEventType(); String name =""; String author=""; String price=""; while(eventType != XmlPullParser.END_DOCUMENT){ String nodeName = xmlPullParser.getName(); switch(eventType){ case XmlPullParser.START_TAG:{ if("name".equals(nodeName)){ name = xmlPullParser.nextText(); }else if("author".equals(nodeName)){ author = xmlPullParser.nextText(); }else if("price".equals(nodeName)){ price = xmlPullParser.nextText(); } break; } case XmlPullParser.END_TAG:{ if("book".equals(nodeName)){ //完成解析 } break; } default: break; } eventType = xmlPullParser.next(); } }catch(Exception e){ e.printStackTrace(); }
还有一题呢?
不需要了,已经过了,谢谢各位