Explain the DefaultHandler processing class that uses sax to parse xml files

Over the course of a thousand years, I have lifted the curtain of years countless times, just to meet you on a calm and watery day, and then get acquainted with each other for a lifetime, a lifetime, a beautiful prose, well, I would rather put this "you "As android;), using sax to parse xml files is the simplest way I have seen to parse xml.

Java code
SAXParserFactory factory = SAXParserFactory.newInstance();    
SAXParser parser = factory.newSAXParser();    
XMLReader xmlReader = parser.getXMLReader();    
xmlReader.setContentHandler(mRSSHandler);    
xmlReader.parse(new InputSource(mStream));  

          What I want to explain here is the factory design pattern used by sax. The parser is obtained through SAXParserFactory, and the xmlReader that parses the xml file is obtained from the parser. However, when the xmlReader reads the streaming xml file, you need to complete the setting of an RSSHandler. RSSHandler is an inherited DefaultHandler, so this article focuses on explaining the DefaultHandler processing class that uses sax to parse xml files. Here I take the parsing of the rss.xml file of the website as an example, let’s first look at the file format of rss.xml:

XML / HTML code
Copy code
<?xml version="1.0" encoding="utf-8" ?>       
<rss version="2.0">      
<channel>      
<item>      
<title>Ubuntu11.04(10.04)安装dos工具dosemu</title>       
<link>http://www.ourunix.org/post/276.html</link>       
<author>[email protected] (walfred)</author>       
<category>玩转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-like win tool after wine, so I will introduce dosemu directly above Installation steps on ubuntu Linux and use it to run DOS games: Contra~~~ </ description >        
</ item >       
</ channel >       
</ rss >   
Copy code

The DefaultHandler processing class inherited by        R SSHandler is specifically designed to parse this file. Let's take a look at the interface we must complete:

Copy code
public  void startDocument () {  
         // Start parsing the document   
    }  
  
    public  void endDocument () {  
         // End of document parsing   
    }  
  
    public  void startElement (String uri, String localName, String qName, Attributes 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 parsing node   
    }  
Copy code

         Generally, the first two methods do not need to process the beginning and end of parsing the document. All our operations are in the parsing node part. We call startElement to start parsing the node, then call characters to save the content of the node, and finally call endElement, so It's just a loop, you can see an example of parsing rss:

Copy code
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;  
      
    //标记当前节点  
    private int currentState;  
      
    public RSSHandler(Context ctx){  
        mContext = ctx;  
        //初始化当前节点标记为0  
        currentState = 0;  
        //数据库接口  
        mRSSDBInterface = new RSSDBInterface(mContext);  
    }  
      
    public void startDocument () {  
        //开始解析文档  
        mRSSItem = new RSSItem();
    }  
  
    public void endDocument () {  
        //文档解析结束  
    }  
  
    public void startElement (String uri, String localName, String qName, Attributes attributes) {  
        //开始解析节点  
        if (localName.equals("channel")){  
            return ;  
        }  
          
        if (localName.equals("item")){  
            //当遇到一个item节点时,就实例化一个RSSItem对象  
            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.equals("category")){  
            currentState = CATEGORY_STATE;  
            return ;  
        }  
          
        if (localName.equals("pubDate")){  
            currentState = PUBDATE_STATE;  
            return ;  
        }  
    }  
      
    public void endElement (String uri, String localName, String qName) {  
        //这是节点解析完成时调用的,这里我们遇到item的时候才调用下面的  
        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;  
        }  
    }  

}

Reprinted from: http://www.cnblogs.com/chenying99/archive/2013/05/14/3077014.html

Guess you like

Origin blog.csdn.net/xifei66/article/details/54893806