In the process of parsing XML files, it is found that the API documentation does not provide detailed description of parsing events, which brings a lot of troubles to parsing XML files, today we have a demo of Event Type Analysis to share with you.
Step 1: Create an Android project file without any changes. You only need to write an xmlTest () method in the onCreate () method. The Code is as follows:
[Java]
Package com. example. xmltest;
Import java. io. IOException;
Import org. xmlpull. v1.XmlPullParser;
Import org. xmlpull. v1.XmlPullParserException;
Import android. app. Activity;
Import android. content. res. XmlResourceParser;
Import android. OS. Bundle;
Public class MainActivity extends Activity {
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
XmlTest ();
}
/**
* Test the event type in the Pull parsing method.
*/
Private void xmlTest (){
// Define the Event Type
Int eventType = 0;
Try {
XmlResourceParser xrp = getResources (). getXml (R. xml. timezones );
// When an xml file is obtained, XmlResourceParser points to the beginning of the document.
EventType = xrp. getEventType ();
// System. out. println ("-------->" + eventType); // view the event Value
While (eventType! = XmlPullParser. END_DOCUMENT ){
Switch (eventType ){
Case XmlPullParser. START_DOCUMENT:
System. out. println ("Start document ");
Break;
Case XmlPullParser. START_TAG:
System. out. println ("Start tag" + xrp. getName ());
Break;
Case XmlPullParser. TEXT:
System. out. println ("Text" + xrp. getText ());
Break;
Case XmlPullParser. END_TAG:
System. out. println ("End tag" + xrp. getName ());
Break;
Default:
Break;
}
EventType = xrp. next ();
// System. out. println ("-------->" + eventType); // view the event Value
}
} Catch (XmlPullParserException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
}
// Determine whether the event type is end of the document
If (eventType = XmlPullParser. END_DOCUMENT ){
System. out. println ("End document ");
}
}
}
Package com. example. xmltest;
Import java. io. IOException;
Import org. xmlpull. v1.XmlPullParser;
Import org. xmlpull. v1.XmlPullParserException;
Import android. app. Activity;
Import android. content. res. XmlResourceParser;
Import android. OS. Bundle;
Public class MainActivity extends Activity {
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
XmlTest ();
}
/**
* Test the event type in the Pull parsing method.
*/
Private void xmlTest (){
// Define the Event Type
Int eventType = 0;
Try {
XmlResourceParser xrp = getResources (). getXml (R. xml. timezones );
// When an xml file is obtained, XmlResourceParser points to the beginning of the document.
EventType = xrp. getEventType ();
// System. out. println ("-------->" + eventType); // view the event Value
While (eventType! = XmlPullParser. END_DOCUMENT ){
Switch (eventType ){
Case XmlPullParser. START_DOCUMENT:
System. out. println ("Start document ");
Break;
Case XmlPullParser. START_TAG:
System. out. println ("Start tag" + xrp. getName ());
Break;
Case XmlPullParser. TEXT:
System. out. println ("Text" + xrp. getText ());
Break;
Case XmlPullParser. END_TAG:
System. out. println ("End tag" + xrp. getName ());
Break;
Default:
Break;
}
EventType = xrp. next ();
// System. out. println ("-------->" + eventType); // view the event Value
}
} Catch (XmlPullParserException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
}
// Determine whether the event type is end of the document
If (eventType = XmlPullParser. END_DOCUMENT ){
System. out. println ("End document ");
}
}
}
Step 2: the above Code uses a timezones. the following code copies the following code to the Android project file --> res --> xml Folder: [html] view plaincopyprint? <? Xml version = "1.0" encoding = "UTF-8"?>
<Timezones>
<Timezone id = "Pacific/Majuro"> majunaro </timezone>
<Timezone id = "Pacific/Midway"> Midway Island </timezone>
</Timezones>
<? Xml version = "1.0" encoding = "UTF-8"?>
<Timezones>
<Timezone id = "Pacific/Majuro"> majunaro </timezone>
<Timezone id = "Pacific/Midway"> Midway Island </timezone>
</Timezones> Part 3: run the program on the simulator. The following information is displayed on LogCat:
The output is analyzed as follows:
1. we can see that the Start document is printed twice, indicating that when the program instantiates the XmlResourceParser object, the default event of the Pull parser is XmlPullParser. START_DOCUMENT, when one XmlResourceParser is executed. after next (), the event still points to XmlPullParser. START_DOCUMENT.
2. Then execute XmlResourceParser. next () to get an event in sequence.
3. Common events include XmlPullParser. START_DOCUMENT, XmlPullParser. START_TAG, XmlPullParser. TEXTXmlPullParser. END_TAG, and XmlPullParser. END_DOCUMENT.