Basic android development tutorial-xml file parsing in three ways

Source: Internet
Author: User

1. sax Method Copy codeThe Code is as follows :/**
* Use sax for parsing
*/
Public class SaxParse {
/**
* Sax Parser
*/
Private SAXParser parser;
Public SaxParse (){
Try {
SAXParserFactory f = SAXParserFactory. newInstance ();
Parser = f. newSAXParser ();
} Catch (ParserConfigurationException e ){
E. printStackTrace ();
} Catch (Exception e ){
E. printStackTrace ();
}
}
Public List <Person> doParse (InputStream is ){
Try {
XmlHandler h = new XmlHandler ();
Parser. parse (is, h );
Return h. getpersons ();
} Catch (Exception e ){
E. printStackTrace ();
}
Return null;
}
/**
* Processor
*/
Class XmlHandler extends DefaultHandler {
List <Person> persons = null;
Person person = null;
// Name of the current element
Private String currEleName;
/**
* This method is triggered by a text node.
*/
Public void characters (char [] ch, int start, int length) throws SAXException {
String str = new String (ch, start, length );
// Name
If ("name". equals (currEleName )){
Person. name = str;
}
Else if ("age". equals (currEleName )){
Person. age = Integer. parseInt (str );
}
}
Public void endDocument () throws SAXException {
}
/**
* Element end
*/
Public void endElement (String uri, String localName, String qName)
Throws SAXException {
If ("person". equals (localName )){
Persons. add (person );
}
// Empty the current element
Else if ("name". equals (currEleName) | ("age". equals (currEleName ))){
This. currEleName = "";
}
}
/**
* Document start event
*/
Public void startDocument () throws SAXException {
Persons = new ArrayList <Person> ();
}
/**
* Element start event
* LocalName: local place name
* Uri: namespace
* QName: qualified name, prefix + local place name
*/
Public void startElement (String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// Instantiate the person object
If ("person". equals (localName )){
Person = new Person ();
Person. id = Integer. parseInt (attributes. getValue (0 ));
}
// Name Element
Else if ("name". equals (localName )){
This. currEleName = "name ";
}
// Name Element
Else if ("age". equals (localName )){
This. currEleName = "age ";
}
}
Public List <Person> getpersons (){
Return persons;
}
}
}

2. dom ModeCopy codeThe Code is as follows :/**
* DOM Parsing
*/
Public class DomParse {
//
Private DocumentBuilder builder;
Public DomParse (){
Try {
DocumentBuilderFactory f = DocumentBuilderFactory. newInstance ();
This. builder = f. newDocumentBuilder ();
} Catch (Exception e ){
E. printStackTrace ();
}
}
Public List <Person> doParse (InputStream is ){
List <Person> persons = new ArrayList <Person> ();
Person person = null;
Try {
Document doc = builder. parse (is );
NodeList list = doc. getElementsByTagName ("person ");
Element ele = null;
For (int I = 0; I <list. getLength (); I ++ ){
Ele = (Element) list. item (I );
Person = new Person ();
Person. id = Integer. parseInt (ele. getAttribute ("id "));
Person. name = getSubElementTextContent (ele, "name ");
Person. age = Integer. parseInt (getSubElementTextContent (ele, "age "));
Persons. add (person );
}
} Catch (Exception e ){
E. printStackTrace ();
}
Return persons;
}
/**
* Obtain the text content in the middle of the specified resource.
*/
Private String getSubElementTextContent (Element ele, String tagName ){
NodeList list = ele. getElementsByTagName (tagName );
Element e = (Element) list. item (0 );
// Obtain the intermediate text node
Return e. getTextContent ();
}
}

3. pull ModeCopy codeThe Code is as follows :/**
* Pull resolution and pull mode. You can manually control whether the next event is triggered.
*/
Public class PullParse {
Public List <Person> doParse (InputStream is ){
List <Person> persons = null;
Person person = null;
Try {
XmlPullParser parser = Xml. newPullParser ();
// Set the resolution data source
Parser. setInput (is, "UTF-8 ");
// Obtain the Event Type
Int eventType = parser. getEventType ();
String eleName = null;
While (eventType! = XmlPullParser. END_DOCUMENT ){
Switch (eventType ){
// Start the document
Case XmlPullParser. START_DOCUMENT:
Persons = new ArrayList <Person> ();
Break;
// Element start
Case XmlPullParser. START_TAG:
EleName = parser. getName ();
If ("person". equals (eleName )){
Person = new Person ();
Person. id = Integer. parseInt (parser. getAttributeValue (0 ));
}
Else if ("name". equals (eleName )){
Person. name = parser. nextText ();
}
Else if ("age". equals (eleName )){
Person. age = Integer. parseInt (parser. nextText ());
}
Break;
// Mark the end
Case XmlPullParser. END_TAG:
EleName = parser. getName ();
If ("person". equals (eleName )){
Persons. add (person );
}
Break;
}
// Manually activate the trigger of the next event
EventType = parser. next ();
}
} Catch (Exception e ){
E. printStackTrace ();
}
Return persons;
}
}

Related Article

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.