In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you how to use XmlPullParser on Android, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!
Using XmlPullParser on Android is an efficient and easy-to-maintain way to parse XML. Android has historically had two classes that implement this interface:
KXmlParser, through XmlPullParserFactory.newPullParser ().
ExpatPullParser, through Xml.newPullParser ().
There is an error in implementing Xml.newPullParser () to call nextText (). NextText () does not always take precedence over END_TAG as mentioned in the document.
As a result, some applications may have bug calling next () or nextTag () for extra calls.
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 ("" + "" + "Waffles" + "Coffee" + "));}
In android4.0, you changed Xml.newPullParser () to return the KxmlParser class, while deleting the ExpatPullParser class. This modifies the bug of nextTag ().
Unfortunately, applications that are currently likely to crash are lower than the android4.0 version, and here is the error message.
Org.xmlpull.v1.XmlPullParserException: expected: END_TAG {null} item (position:START_TAG @ 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)
The solution is to jump to nextTag () only after calling nextText (), only if the current location is not END_TAG.
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);}
The above code parses all xml versions correctly, and if the application uses nextText () extensively, use the following helper methods where nextText () is used.
Private String safeNextText (XmlPullParser parser) throws XmlPullParserException, IOException {String result = parser.nextText (); if (parser.getEventType ()! = XmlPullParser.END_TAG) {parser.nextTag ();} return result;}
Using a single XmlPullParse simplifies our maintenance and allows us to spend more energy on improving the performance of the system.
The above is all the content of the article "how to use XmlPullParser on Android". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.