本文转自:http://samwong.iteye.com/blog/1000403
android xml 的解析.
1.普通型
- <?xml version=”1.0″ ?>
- <statuses>
- <status>
- <source1>a</source1>
- <source2>a</source2>
- <source2>a</source2>
- </status>
- <statuses>
- </xml>
- try{
- xpp.setInput( new StringReader ( “xml string” ) );
- XmlPullParser parser = XmlPullParserFactory.newInstance()
- .newPullParser();
- int eventType = parser.getEventType();
- while (eventType != XmlPullParser.END_DOCUMENT) {
- if (eventType == XmlPullParser.START_TAG) {
- String name = parser.getName();
- Log.v(“tag”,parser.getName()+”=”+parser.next());
- }
- eventType = parser.next();
- }
- } catch (XmlPullParserException e) {
- } catch (Exception e) {
- }
- <?xml version=”1.0″ encoding=”UTF-8″ standalone=”no”?>
- <statuses>
- <status>
- <source1>a</source1>
- <source2>a</source2>
- <source2>a</source2>
- <user>
- <source1>a</source1>
- <source2>a</source2>
- <source2>a</source2>
- </user>
- </status>
- <status>
- <source1>a</source1>
- <source2>a</source2>
- <source2>a</source2>
- <user>
- <source1>a</source1>
- <source2>a</source2>
- <source2>a</source2>
- </user>
- </status>
- <statuses>
- </xml>
- try{
- XmlPullParser parser = XmlPullParserFactory.newInstance()
- .newPullParser();
- xpp.setInput( new StringReader ( xml string ) );
- parser.next();
- parser.require(XmlPullParser.START_TAG, null, “statuses”);
- while (parser.nextTag() != XmlPullParser.END_TAG) {
- parser.require(XmlPullParser.START_TAG, null, “status”);
- for (int i=0;i<3;i++){
- parser.nextTag();
- Log.v(“tag”,parser.getName()+”=”+ parser.nextText());
- }
- while (parser.nextTag() != XmlPullParser.END_TAG) {
- parser.require(XmlPullParser.START_TAG, null, “user”);
- while (parser.nextTag() != XmlPullParser.END_TAG) {
- String name = parser.getName();
- String text2 = parser.nextText();
- Log.v(“tag”,”text2″+text2);
- parser.require(XmlPullParser.END_TAG, null, name);
- }
- parser.require(XmlPullParser.END_TAG, null, “user”);
- }
- parser.require(XmlPullParser.END_TAG, null, “status”);
- }
- parser.require(XmlPullParser.END_TAG, null, “statuses”);
- parser.next();
- parser.require(XmlPullParser.END_DOCUMENT, null, null);
- // global.userinfo.dump();
- } catch (XmlPullParserException e) {
- } catch (Exception e) {
- }
- for (int i=0;i<3;i++){
- parser.nextTag();
- Log.v(“tag”,parser.getName()+”=”+ parser.nextText());
- }
开发过程中遇到XmlPullParser.netText()方法造成的Bug
解决如下:
在Android上使用XmlPullParser是一中高效率和易维护解析XML的方法 。Android已经在历史上有两个实现这个接口实现类:
KXmlParser,通过XmlPullParserFactory.newPullParser() 。
ExpatPullParser,通过Xml.newPullParser() 。
实现Xml.newPullParser()
调用nextText()有个错误,nextText()
并不总是向文档中所提到优先于END_TAG执行
。
因此,一些应用可能会出现bug在额外调用next()或nextTag();
- throws XmlPullParserException, IOException {
- XmlPullParser parser = Xml.newPullParser();
- parser.setInput(reader);
- parser.nextTag();
- parser.require(XmlPullParser.START_TAG, null, "menu");
- while (parser.nextTag() == XmlPullParser.START_TAG) {
- parser.require(XmlPullParser.START_TAG, null, "item");
- String itemText = parser.nextText();
- parser.nextTag(); // this call shouldn’t be necessary!
- parser.require(XmlPullParser.END_TAG, null, "item");
- System.out.println("menu option: " + itemText);
- }
- parser.require(XmlPullParser.END_TAG, null, "menu");
- }
- public static void main(String[] args) throws Exception {
- new Menu().parseXml(new StringReader("<?xml version='1.0'?>"
- + "<menu>"
- + " <item>Waffles</item>"
- + " <item>Coffee</item>"
- + "</menu>"));
- }
不幸的是,当前可能会崩溃的应用程序都是低于android4.0版本的,下面是错误信息。
- org.xmlpull.v1.XmlPullParserException: expected: END_TAG {null}item (position:START_TAG <item>@1:37 in java.io.StringReader@40442fa8)
- at org.kxml2.io.KXmlParser.require(KXmlParser.java:2046)
- at com.publicobject.waffles.Menu.parseXml(Menu.java:25)
- at com.publicobject.waffles.Menu.main(Menu.java:32)
- while (parser.nextTag() == XmlPullParser.START_TAG) {
- parser.require(XmlPullParser.START_TAG, null, "item");
- String itemText = parser.nextText();
- if (parser.getEventType() != XmlPullParser.END_TAG) {
- parser.nextTag();
- }
- parser.require(XmlPullParser.END_TAG, null, "item");
- System.out.println("menu option: " + itemText);
- }
- private String safeNextText(XmlPullParser parser)
- throws XmlPullParserException, IOException {
- String result = parser.nextText();
- if (parser.getEventType() != XmlPullParser.END_TAG) {
- parser.nextTag();
- }
- return result;
- }