One thousand years of time, I have set off the curtain of the years, just to meet you on a peaceful day, and then get to know each other, love your life, forever, beautiful prose, okay, I 'd rather use this "you" as Android;). Using Sax to parse XML files is the simplest way I have ever seen.
Java code
SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); XMLReader xmlReader = parser.getXMLReader(); xmlReader.setContentHandler(mRSSHandler); xmlReader.parse(new InputSource(mStream));
It should be noted that the factory design mode used by sax gets the parser of the parser through saxparserfactory, and obtains the xmlreader that parses the XML file from the parser, however, when xmlreader reads a stream XML file, it needs to complete an rsshandler setting. rsshandler is the inherited defaulthandler. Therefore, this article focuses on the use of Sax to parse the defaulthandler processing class of XML files. Here, I will take the RSS. xml file of the website as an example. Next we will first look at the file format of RSS. xml:
XML/html code
<? XML version = "1.0" encoding = "UTF-8"?> <RSS version = "2.0"> <channel> <item> <title> ubuntu11.04 (10.04) install dostool dosemu </title> <link> http://www.ourunix.org/post/276.html </link> <author> ourunix@163.com (walfred) </author> <Category> play Linux </Category> <pubdate> Mon, 16 Jan 2012 22:54:53 + 0800 </pubdate> <comments/> <description> after reading the introduction, I found that this is a Linux win tool after wine, so now we will introduce how to install dosemu on Ubuntu Linux and how to use it to run dos games: Soldado ~~~ </Description> </item> </channel> </RSS>
The defaulthandler processing class inherited by rsshandler is dedicated to parsing this file. Let's look at the interface we must complete:
Public void startdocument () {// start parsing document} public void enddocument () {// end of document parsing} public void startelement (string Uri, string localname, string QNAME, attributes) {// start parsing node} public void characters (char [] CH, int start, int length) {// save node content} public void endelement (string Uri, string localname, string QNAME) {// end resolution node}
In general, the first two methods do not need to be processed before parsing and ending the parsing document. All our operations are in the parsing node section. We call startelement to start the parsing node, then, call characters to save the node content and call endelement. This is a loop. You can see the example of parsing RSS:
Public class rsshandler extends defaulthandler {private context mcontext; private rssitem mrssitem; private rssdbinterface mrssdbinterface; private final int title_state = 1; private final int author_state = 2; private final int link_state = 3; private Final int description_state = 4; private final int category_state = 5; private final int pubdate_state = 6; // mark the current node private int currentstate; Publ IC rsshandler (context CTX) {mcontext = CTX; // mark the current initialization node as 0 currentstate = 0; // database interface mrssdbinterface = new rssdbinterface (mcontext );} public void startdocument () {// start parsing document mrssitem = new rssitem ();} public void enddocument () {// end of document parsing} public void startelement (string Uri, string localname, string QNAME, attributes) {// start to parse the node if (localname. equals ("channel") {return;} If (Local Name. equals ("item") {// when an item node is encountered, an rssitem object mrssitem = new rssitem (); return;} If (localname. equals ("title") {currentstate = title_state; return;} If (localname. equals ("author") {currentstate = author_state; return;} If (localname. equals ("Description") {currentstate = description_state; return;} If (localname. equals ("Link") {currentstate = link_state; return;} If (localname. E Quals ("category") {currentstate = category_state; return;} If (localname. equals ("pubdate") {currentstate = pubdate_state; return ;}} public void endelement (string Uri, string localname, string QNAME) {// This is called when node resolution is complete, the following if (localname. equals ("item" & mrssitem! = NULL) {contentvalues values = new contentvalues (); values. put (rssdbinfo. columns. _ title, mrssitem. gettitle (); values. put (rssdbinfo. columns. _ author, mrssitem. getauthor (); values. put (rssdbinfo. columns. _ category, mrssitem. getcategory (); values. put (rssdbinfo. columns. _ description, mrssitem. getdescription (); values. put (rssdbinfo. columns. _ link, mrssitem. getlink (); values. put (rssdbinfo. columns. _ pubdate, mrssitem. getpubdate (); values. put (rssdbinfo. columns. _ isread, rssutils. articale_unread); mrssdbinterface. insertrsstodb (values) ;}} public void characters (char [] CH, int start, int length) {string thestring = new string (CH, start, length); Switch (currentstate) {Case title_state: mrssitem. settitle (thestring); currentstate = 0; break; Case author_state: mrssitem. setauthor (thestring); currentstate = 0; break; Case link_state: mrssitem. setlink (thestring); currentstate = 0; break; Case description_state: mrssitem. setdescription (thestring); currentstate = 0; break; Case category_state: mrssitem. setcategory (thestring); currentstate = 0; break; Case pubdate_state: mrssitem. setpubdate (thestring); currentstate = 0; break ;}}}