Android [intermediate tutorial] Chapter 6 XML Parser

Source: Internet
Author: User

 

Next, let's take a look at the SAX Parser. In fact, it's similar to the PULL parser. In fact, it's enough to learn and master an XML parser to complete the project, of course, it is not a bad thing to learn more. What do you mean? Come on! Let's take the data of Tang Miao's migration as an example.

 

 

<? Xml version = "1.0" encoding = "UTF-8"?>

<Persons>

<Person id = "1">

<Status> big brother </status>

<Name> Sun Wukong </name>

<Tool> golden hoop </tool>

<Number> killed 50 monsters </number>

</Person>

<Person id = "2">

<Status> Second Brother </status>

<Name> pig Bajie </name>

<Tool> nine-tooth dingtalk </tool>

<Number> killed 40 monsters </number>

</Person>

<Person id = "3">

<Status> three younger siblings </status>

<Name> Sha monk </name>

<Tool> demon master pin </tool>

<Number> killed 30 monsters </number>

</Person>

</Persons>

 

 

The xml file is the same as that in the previous chapter. If you are not clear about it, refer to the previous chapter.

 

Android [intermediate tutorial] Chapter 5 PULL Parser for XML Parsing

Here I mainly put the source code of SAXHandler.

 

 

Import java. util. ArrayList;

Import java. util. List;

 

Import org. xml. sax. Attributes;

Import org. xml. sax. SAXException;

Import org. xml. sax. helpers. DefaultHandler;

 

Public class SaxHandler extends DefaultHandler

{

Private List <Person> persons;

Private Person person;

Private String tagName; // indicates the name of the previous Node during resolution.

Public List <Person> getPersons (){

Return persons;

} Www.2cto.com

/**

* Node Processing

*/

@ Override

Public void characters (char [] ch, int start, int length)

Throws SAXException

{

String data = new String (ch, start, length );

If ("status". equals (tagName )){

Person. setStatus (data );

}

If ("name". equals (tagName )){

Person. setName (data );

}

If ("tool". equals (tagName )){

Person. setTool (data );

}

If ("number". equals (tagName )){

Person. setNumber (data );

}

}

 

/**

* Element end

*/

@ Override

Public void endElement (String uri, String localName, String qName)

Throws SAXException

{

If ("person". equals (localName )){

Persons. add (person );

Person = null;

}

TagName = null;

}

/**

* Document start

*/

@ Override

Public void startDocument () throws SAXException

{

Persons = new ArrayList <Person> ();

}

/**

* Element start

*/

@ Override

Public void startElement (String uri, String localName, String qName,

Attributes attributes) throws SAXException

{

If ("person". equals (localName )){

Person = new Person ();

Person. setId (attributes. getValue ("id "));

}

TagName = localName; // assign the name of the node being parsed to the tagName

}

}

The following is the source code in the Activity:

 

 

Import java. io. File;

Import java. io. FileInputStream;

Import java. util. ArrayList;

Import java. util. HashMap;

Import java. util. List;

Import java. util. Map;

 

Import javax. xml. parsers. SAXParser;

Import javax. xml. parsers. SAXParserFactory;

 

Import android. app. Activity;

Import android. OS. Bundle;

Import android. OS. Environment;

Import android. widget. ListView;

Import android. widget. SimpleAdapter;

 

Public class PullActivity extends Activity

{

Private ListView listView;

Private SimpleAdapter adapter;

 

@ Override

Protected void onCreate (Bundle savedInstanceState)

{

// TODO Auto-generated method stub

Super. onCreate (savedInstanceState );

SetContentView (R. layout. xml_handler );

ListView = (ListView) findViewById (R. id. xml_list );

Try

{

// Adaptive Method

GetAdapter ();

} Catch (Exception e)

{

E. printStackTrace ();

}

ListView. setAdapter (adapter );

}

 

// Custom adaptation method

Private void getAdapter () throws Exception

{

List <Map <String, String> lists = new ArrayList <Map <String, String> ();

// This part is used for testing.

File SD_Files = Environment. getExternalStorageDirectory ();

String file_path = SD_Files.getName () + File. separator + "persons. xml ";

// FileInputStream input = new FileInputStream (new File (file_path ));

// PullHandler pullHandler = new PullHandler (input );

// List <Person> persons = pullHandler. getPersons ();

FileInputStream FCM = new FileInputStream (new File (file_path ));

SAXParser parser = SAXParserFactory. newInstance (). newSAXParser ();

SaxHandler saxHandler = new SaxHandler ();

Parser. parse (FCM, saxHandler );

List <Person> persons = saxHandler. getPersons ();

// Convert the data in persons to ArrayList <Map <String, String>, because SimpleAdapter uses this type of data for adaptation.

Map <String, String> map;

For (Person p: persons ){

Map = new HashMap <String, String> ();

Map. put ("id", p. getId ());

Map. put ("status", p. getStatus ());

Map. put ("name", p. getName ());

Map. put ("tool", p. getTool ());

Map. put ("number", p. getNumber ());

Lists. add (map );

}

// Key in HashMap <String, String>

String [] from = {"id", "status", "name", "tool", "number "};

// Control ID in list_item.xml

Int [] to = {R. id. item_id, R. id. item_status, R. id. item_name, R. id. item_tool, R. id. item_number };

Adapter = new SimpleAdapter (this, lists, R. layout. handler_list_item, from, );

}

}

Other parsing methods are the same as PULL parsing. Here we only replace PULL parsing with SAX parsing. In the source code, we replace the three lines of code in the middle with the code in the SAX parsing ,:

Look, they are all the same. Next chapter will introduce the DOM parser. I hope you can study it well. Thank you.

From: kangkangz4 Column

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.