Beauties. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <beauties> <br/> <beauty> <br/> <Name> Lin Zhiling </Name> <br/> <age> 28 </age> <br /> </beauty> <br/> <Name> Yang Mi </Name> <br/> <age> 23 </age> <br/> </beauty> </P> <p> </beauties> <br/>
Main. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <textview <br/> Android: id = "@ + ID/textview" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> </linearlayout> <br/>
Activity Code:
Package cn.com. saxtest; </P> <p> Import Java. io. inputstream; <br/> Import Java. util. arraylist; </P> <p> Import javax. XML. parsers. saxparser; <br/> Import javax. XML. parsers. saxparserfactory; </P> <p> Import Org. XML. sax. attributes; <br/> Import Org. XML. sax. saxexception; <br/> Import Org. XML. sax. helpers. defaulthandler; </P> <p> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. util. log; <B R/> Import android. widget. textview; </P> <p>/** <br/> * @ author chenzheng_java <br/> * @ description Parse XML using sax <br/> * @ since 2010/03/04 <br/> */<br/> public class saxparseractivity extends activity {</P> <p> private arraylist <beauty> beautylist = new arraylist <beauty> (); <br/> private Beauty = NULL; </P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> Super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); </P> <p> saxparserfactory factory = saxparserfactory. newinstance (); <br/> try {<br/> saxparser parser = factory. newsaxparser (); <br/> inputstream = This. getclassloader () <br/>. getresourceasstream ("beauties. XML "); <br/> parser. parse (inputstream, new mydefaulthandler (); </P> <p> string result = ""; <br/> for (Beauty: BEA Utylist) {<br/> result + = "/N" + beauty. tostring (); <br/>}</P> <p> textview = (textview) findviewbyid (R. id. textview); <br/> textview. settext (result); </P> <p >}catch (exception e) {<br/> E. printstacktrace (); <br/>}</P> <p>/** <br/> * @ author chenzheng_java <br/> * @ description note, here is the internal class used. Some friends may have questions and do not need internal classes. The answer is: Of course. <Br/> * The purpose of using internal classes here is to improve efficiency. If you like the source code, you will find that the source code uses a lot of internal classes. <Br/> */<br/> private class mydefaulthandler extends defaulthandler {</P> <p> // stores the last element read so far localname <br/> private string currentelementname = ""; </P> <p>/** <br/> * characters (char ch [], int start, int length) is executed when parsing text in XML. Ch <br/> * This array stores the string form of the entire XML file. Start is the starting position of the currently parsed text in the entire XML string file. <br/> * length it is the length of the text content currently parsed. We can know from the above introduction, we can use new <br/> * string (CH, start, length) method to obtain the parsed text content <br/> */<br/> @ override <br/> Public void characters (char [] CH, int start, int length) <br/> throws saxexception {<br/> log. I ("currentelementname", currentelementname); <br/> string textcontent = new string (CH, start, length); </P> <p> If (C Urrentelementname. Equals ("name") & textcontent! = NULL &&! Textcontent. trim (). equals ("") {<br/> log. I ("textcontent name", textcontent); <br/> beauty. setname (textcontent); </P> <p >}< br/> If (currentelementname. equals ("Age") & textcontent! = NULL &&! Textcontent. trim (). equals ("") {<br/> log. I ("textcontent age", textcontent); <br/> beauty. setage (textcontent ); <br/>}</P> <p>/** <br/> * triggered when parsing to the end of the XML document <br/> */<br/> @ override <br/> Public void enddocument () throws saxexception {<br/>}</P> <p>/** <br/> * triggered when the element is parsed to the end <br/> */<br/> @ override <br/> Public void endelement (string Uri, string localname, string QNAME) <br/> throws saxexception {<br/> If (Localname. equals ("beauty") {<br/> beautylist. add (beauty); <br/> log. I ("beauty", beauty. tostring ()); </P> <p >}< br/>}</P> <p>/** <br/> * triggered when XML parsing starts. <br/> */< br/> @ override <br/> Public void startdocument () throws saxexception {</P> <p >}</P> <p>/** <br/> * trigger startelement (string Uri, string localname, string QNAME, <br/> * attributes) URI: The namespace value. This parameter is null if it is not explicitly specified or when the namespace is not used. <br/> * Localname: the name of the element, or the name of the tag. For example, the name in <Name> is localname QNAME: <br/> * and the unique localname is <br/> *. When the label has a namespace, the data returned by this value is a fully qualified name. For example, in <Chen: Name>, localname is name <br/> * And QNAME is Chen: Name attributes: attribute object contained in the element. If no property exists, an empty property object <br/> */<br/> @ override <br/> Public void startelement (string Uri, string localname, string QNAME, <br/> attributes) throws saxexception {<br/> currentelementname = localname; <br/> If (localname. equals ("beauty") {<br/> beauty = new beauty (); <br/>}</P> <p>/** <br/> * <br/> * @ author chenzheng internal classes are used here for efficiency consideration, internal classes are more efficient and space-saving than single bean classes <br/> */<br/> private class beauty {<br/> string name; <br/> string age; </P> <p> Public String getname () {<br/> return name; <br/>}</P> <p> Public void setname (string name) {<br/> This. name = Name; <br/>}</P> <p> Public String getage () {<br/> return age; <br/>}</P> <p> Public void setage (string age) {<br/> This. age = age; <br/>}</P> <p> @ override <br/> Public String tostring () {<br/> return "Beauty information [age =" + getage () + ", name =" + getname () + "]"; <br/>}</P> <p>}
After running, you will get the desired results,
Code structure:
Bytes ----------------------------------------------------------------------------------------------------------
Next, let's discuss some nonsense:
Sax parsing 1. SAX (simple application interface for XML) is a set of programming interfaces. It adopts the observer mode to treat XML files as data of a text stream and triggers a series of events when reading XML elements. This is a typical application of the observer mode.
2. when an XML file is loaded using sax, his operations are like opening a "sequential file upload stream". When reading the start tag of the XML element, a series of events are generated when the end tag and content tag are generated.
For example, a simple XML file: <Hello> <message> Hello XML! </Message> </Hello>
It will trigger: startdocument, startelement, characters, endelement, and enddocument. You only need to write these event handlers to parse the XML file.
3. Sax can efficiently use the memory, because sax only reads the content of XML files in sequence and does not fully load XML files, which is more efficient than dom processing.
However, Sax can only read the content of an XML file, but cannot modify the content of the XML file or randomly access the XML element.
4. There are four processors in Sax to implement: contenthandler, dtdhandler, entityresolver, and errorhandler to process different events. This is troublesome,
Fortunately, Sax defines a defaulthandler class to implement these functions. We only need to define the event processing method in defaulthandler and register it with xmlreader, while saxparser encapsulates the implementation class of xmlreader,
Saxparser is provided by saxparserfactory. Therefore, we only use the following classes: saxparserfactory, saxparser, and defaulthandler.
5. Analysis Steps of sax:
(1) Write a class that inherits defaulthandler and implements its own event processing method.
(2) create saxparserfactory in the main program
(3) You can set the parameters of this factory.
(4) Get saxparser from this factory
(5) Parse XML files