Learning Experience record

Source: Internet
Author: User

Today is July 22, July 31, 2013. Starting from this afternoon, I will start a four-month study on Android. I want to log my learning life. My goal is to be able to meet the enterprise's employment requirements after I finish learning, and to enter the enterprise to do things. Become an excellent Android developer.

Today, we learned how to parse Java's sax, which is composed of five events.

Resolution Method: Set parsing, Dom parsing,

Arraylist set, which is different from array:

The purpose of sax Parsing is to abstract the concept of an object, put the assigned object into a set, and assign a value to the object.

Sax parsing uses events as a mechanism to divide XML files into five types of events:

Document start event: <? XML version "1.0" encoding = "UTF-8"?>, Initialization

Element start event: <person>

Character data event: attribute value,

Element end event: </person>

Document end event: read to the end, no content.

The parser must be provided by the factory.

URL: namespace, localname: QNAME: Current element node, attributel attribute

1. Sax (Simple API for XML): A Row-by-row read that triggers an event. (This XML document is not loaded into memory)
2. Dom (Document Object Model): Read all XML documents to the memory to form a DOM tree,
If I want to obtain an attribute data of this XML document, I will find the node directly to obtain the value.

Person. Java file:

package com.sax;public class Person {private int id;private int age;private String name;public int getId() {return id;}public void setId(int id) {this.id = id;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}}

Person. xml:

<? XML version = "1.0" encoding = "UTF-8"?> <Persons> <person id = "1"> <Name> Wang honglei </Name> <age> 21 </age> </person> <person id = "2"> <Name> Haha </Name> <age> 22 </age> </person> </persons>

Saxparsertest. Java

Package COM. sax; import Java. io. ioexception; import Java. util. arraylist; import javax. XML. parsers. parserconfigurationexception; import javax. XML. parsers. saxparser; import javax. XML. parsers. saxparserfactory; import Org. XML. sax. saxexception; import Org. XML. sax. xmlreader; public class saxparsertest {public static void main (string [] ARGs) {try {saxparserfactory factory = saxparserfactory. newinstance (); saxparser parser = factory. newsaxparser (); // create the callback parser interface xmlreader reader = parser. getxmlreader (); // create a parsing mechanism saxdefaulthandler handler = new saxdefaulthandler (); // Add the event mechanism to the reader interface. setcontenthandler (handler); // locate the XML file and parse the reader. parse ("src/COM/sax/person. XML "); arraylist <person> List = handler. getlist (); For (person: List) {system. out. println ("ID =" + person. GETID () + ""); system. out. println ("age =" + person. getage () + ""); system. out. println ("name =" + person. getname () + "") ;}} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();} catch (parserconfigurationexception e) {// todo auto-generated catch blocke. printstacktrace ();} catch (saxexception e) {// todo auto-generated catch blocke. printstacktrace ();}}}

Saxdefaulthandler. Java

Package COM. sax; import Java. util. arraylist; import Org. XML. sax. attributes; import Org. XML. sax. saxexception; import Org. XML. sax. helpers. defaulthandler; public class saxdefaulthandler extends defaulthandler {private person P; private string data; private arraylist <person> list; Public arraylist <person> getlist () {return list ;} public void setlist (arraylist <person> List) {This. list = List ;}@ override // document start event public void startdocument () throws saxexception {// initialization operation list = new arraylist <person> ();} @ overridepublic void enddocument () throws saxexception {}@ override // element start event/* URL: namespace * localname: local file * QNAME: Current element name * attribute: attribute Set of the current Element Node */Public void startelement (string Uri, string localname, string QNAME, attributes) throws saxexception {If ("person ". equals (QNAME) {P = new person (); string idname = attributes. getvalue (0); p. setid (integer. parseint (idname) ;}@ override // element end event public void endelement (string Uri, string localname, string QNAME) throws saxexception {// assign a value to the object, put the object in the list if ("name ". equals (QNAME) {P. setname (data);} else if ("age ". equals (QNAME) {P. setage (integer. parseint (data);} else if ("person ". equals (QNAME) {list. add (p) ;}@ override // character data event public void characters (char [] CH, int start, int length) throws saxexception {// value of the text node data = new string (CH, start, length );}}

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.