Android pull Parsing
Pull parsing XML
The XmlPullParser parser runs in a similar way as the SAX Parser. it provides similar events (Start Element and end element), but uses parser. the next () method to extract them. the event is sent as a numeric code, so different processing can be performed based on different event code values. through parser. getEventType () method to obtain the code value of the event (for example, XmlPullParser. START_DOCUMENT, XmlPullParser. START_TAG, XmlPullParser. END_TAG ).
When an element is present, you can call the getAttributte () method of XmlPullParser to obtain the attribute value. You can also call its nextText () method to obtain the value of the current node.
Specific ideas:
* Build a PullXmlParser parser
XmlPullParser parser = Xml. newPullParser ();
* Register the xml document in the parser.
Parser. setInput ();
* Trigger the event to obtain the event type code
Int event = parser. getEventType ();
* Perform event-specific analysis
"Name". equals (parser. getName (); // determines whether the start label element is name.
Parser. getAttributeValue (0); // obtain the tag attribute value.
Parser. nextText (); // get the value of the next Text node
* Trigger and enter the next event
Event = parser. next ();
Pull Parsing is different from Sax parsing:
(1) pull reads the xml file and triggers the corresponding event. The call method returns a number.
(2) pull can control in the program where it is to be parsed to stop parsing.
Advantage: it is not a single load and can be stopped in the middle
* ************ Code for pull parsing *************
1 public class PullparserActivity extends Activity {2 private TextView mtextview; 3 private ListView mlistview; 4 5 @ Override 6 protected void onCreate (Bundle savedInstanceState) {7 8 super. onCreate (savedInstanceState); 9 setContentView (R. layout. domparser); 10 mtextview = (TextView) findViewById (R. id. textView1); 11 mlistview = (ListView) findViewById (R. id. listView1); 12 mtextview. setText ("pull parsing x Ml "); 13 14 List <Student> list = parser (); 15 ArrayAdapter <Student> adapter = new ArrayAdapter <Student> (this, 16 android. r. layout. simple_list_item_1, list); 17 mlistview. setAdapter (adapter); 18 19} 20 21 private List <Student> parser () {22 List <Student> list = new ArrayList <Student> (); 23 // obtain the pull parser 24 XmlPullParser parser = Xml. newPullParser (); 25 try {26 // load xml file 27 parser. setInput (Pullpar SerActivity. this. getClassLoader () 28. getResourceAsStream ("student. xml ")," UTF-8 "); 29 // c. trigger event, get event type code 30 int event = parser. getEventType (); 31 Student student = null; 32 33 while (event! = XmlPullParser. END_DOCUMENT) {34 switch (event) {35 case XmlPullParser. START_DOCUMENT: // The document starts with 36 break; 37 case XmlPullParser. START_TAG: // start tag 38 if ("student ". equals (parser. getName () {39 student = new Student (); 40 student. setId (Integer. valueOf (parser 41. getAttributeValue (0); 42 43} 44 Log. I ("TAG", "infor"); 45 if (student! = Null) {46 if ("name ". equals (parser. getName () {47 student. setName (parser. nextText (); 48} else if ("age ". equals (parser. getName () {49 student. setAge (Integer. valueOf (parser 50. getAttributeValue (0); 51} 52} 53 break; 54 case XmlPullParser. END_TAG: // end tag 55 if ("student ". equals (parser. getName () {56 list. add (student); 57 student = null; 58} 59 Log. I ("TAG", "infor"); 60 break; 61 case XmlPullParser. TEXT: // TEXT parsing 62 break; 63} 64 event = parser. next (); 65 66} 67} catch (Exception e) {68 e. printStackTrace (); 69} 70 71 return list; 72} 73}
Android pull Parsing
If your content is an input stream, you can pass category or area as parameters to get their content as a list and return it. You can also input other node names,
ArrayList <String> parse (InputStream inStream, String tag) throws Exception {
ArrayList <String> valueList = new ArrayList <String> ();
XmlPullParser xmlParser = XmlPullParserFactory. newInstance (). newPullParser ();
XmlParser. setInput (inStream, "UTF-8 ");
Int eventType = XmlPullParser. END_DOCUMENT;
EventType = xmlParser. getEventType ();
String currTag = null;
While (eventType! = XmlPullParser. END_DOCUMENT ){
Switch (eventType ){
Case XmlPullParser. END_TAG:
Break;
Case XmlPullParser. START_TAG:
CurrTag = xmlParser. getName ();
If (tag. equals (currTag )){
Int type = 0;
While (type = xmlParser. next ())! = XmlPullParser. END_DOCUMENT ){
If (type = XmlPullParser. TEXT ){
ValueList. add (xmlParser. getText (). trim ());
Break;
}
}
}
Break;
Case XmlPullParser. TEXT:
Break;
}
}
Return valueList;
}
Android pull parses the same tag name in xml
Pull Parsing is similar to Sax parsing, and both are lightweight parsing.
I used to only use Sax for parsing, but the principle is the same.
Parse and determine if it is <> Use getAttributeValue (0) in angle brackets)
0 is the element at the first position, which is equivalent to your title
If the names are the same, it is difficult to determine the name to be parsed.
Generally, there won't be such troublesome applications.