XML parsing for android

Source: Internet
Author: User

XML parsing for android

The so-called SAX is the simple API for XML, which is a simple API for parsing XML files.

The working principle of XML parsing by SAX is to scan documents sequentially. When the scanning ends and begins, the elements start and end, and the corresponding response function is triggered. It is an event-driven parsing method. It can stop parsing documents at any time.

Through this figure, we can get a rough idea of the parsing process.

Private void parse (String xmlString, List
  
   
Infos) {// SAXParserFactory is a factory-type class SAXParserFactory saxParserFactory = SAXParserFactory. newInstance (); try {XMLReader reader = saxParserFactory. newSAXParser (). getXMLReader (); reader. setContentHandler (new Mp3ListContentHandler (infos); reader. parse (new InputSource (new StringReader (xmlString);} catch (Exception e) {e. printStackTrace ();}}
  

To parse an XML file using SAX, first obtain a SAXParserFactory object, then obtain an XMLreader object, and then pass the class Object of the self-implemented Contenthandler interface to the setContenthandler method of XMLreader, finally, call the parse method of XMLreader to generate Inputsource for the XML file to be parsed.

The key to parsing XML files is to implement the Contenthandler interface class. In this class, we can parse XML files as needed, use and store the content under Different tags as needed.

/*** The XML parsing class parses the XML file by inheriting DefualtHandler and rewriting the method in the class * the following uses the SAX parsing method, but does not select the implementation interface, but uses the adapt mode, inherit from a ContentHandler interface class * @ author longkai **/public class Mp3ListContentHandler extends DefaultHandler {private List
  
   
Infos = null; private Mp3Info mp3Info = null; private String tagname = null; public Mp3ListContentHandler (List
   
    
Infos) {super (); this. infos = infos;}/** (non-Javadoc) * @ see org. xml. sax. helpers. defaultHandler # characters (char [], int, int) * process file content */@ Override public void characters (char [] ch, int start, int length) throws SAXException {String info = new String (ch, start, length); if (tagname. equals ("id") {mp3Info. setId (info);} else if (tagname. equals ("mp3.name") {mp3Info. setMp3name (info);} else if (tagname. equals ("mp3.size") {mp3Info. setMa3size (info);} else if (tagname. equals ("lrc. name ") {mp3Info. setLrcname (info);} else if (tagname. equals ("lrc. size ") {mp3Info. setLrcsize (info) ;}@override public void endDocument () throws SAXException {super. endDocument () ;}@ Override public void endElement (String uri, String localName, String qName) throws SAXException {// if the end tag name is set, implement the specific method if (localName. equals ("resource") {infos. add (mp3Info);} tagname = "" ;}@ Override public void startDocument () throws SAXException {}@ Override public void startElement (String uri, String localName, String qName, attributes attributes) throws SAXException {// each time a new tag is parsed, the tag name is identified and different methods are called based on different tag names. this. tagname = localName; if (tagname. equals ("resource") {mp3Info = new Mp3Info ();}}}
   
  

Through this class, we mainly implement parsing an Mp3resource. the XML file reads the name, size, id, and name size of the mp3 file from the file and stores the file in a list. The following is an XML file:


  
      
           
    
     0001
            
    
     a1.mp3
            
    
     10033011
            
    
     a1.lrc
            
    
     9053
        
       
           
    
     0002
            
    
     a2.mp3
            
    
     10033011
            
    
     a2.lrc
            
    
     9053
        
   
  

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.