Steps for parsing xml files

Source: Internet
Author: User
Tags xml parser

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.