1. Parser for generating xml files
XmlPullParser parser = Xml. newPullParser ();
2. Set the encoding format for the parser to read stream objects.
Parser. setInput (is, "UTF-8 ");
3. Set the location to be stored after parsing the xml file
List <WeatherInfo> weatherInfo = null;
WeatherInfo info = null;
4. Define the event types parsed by the parser
Int type = parser. getEventType ();
5. Set the criteria for determining the parser's stop parsing.
While (type! = XmlPullParser. END_DOCUMENT)
{
Switch (type)
{
Case XmlPullParser. START_TAG:
If ("infos". equals (parser. getName ()))
{
// The start tag is resolved to the global start tag.
WeatherInfo = new ArrayList <WeatherInfo> ();
}
Else if ("city". equals (parser. getName ()))
{
Info = new WeatherInfo ();
String idStr = parser. getAttributeValue (0 );
Info. setId (Integer. parseInt (idStr ));
}
Else if ("temp". equals (parser. getName ()))
{
Info. setTemp (parser. nextText ());
}
Else if ("weather". equals (parser. getName ()))
{
Info. setWeather (parser. nextText ());
}
Else if ("wind". equals (parser. getName ()))
{
Info. setWind (parser. nextText ());
}
Else if ("name". equals (parser. getName ()))
{
Info. setName (parser. nextText ());
}
Else if ("pm". equals (parser. getName ()))
{
Info. setPm (parser. nextText ());
}
Break;
6. Set the tag ending with the xml parser and store the parsed content in the collection.
Case XmlPullParser. END_TAG:
If ("city". equals (parser. getName ()))
{
// The information of a city has been processed.
WeatherInfo. add (info );
Info = null;
}
Break;
}
Type = parser. next ();
}
7. Return the parsed data
Return weatherInfo;
Steps for parsing xml files