XML parsing of Android

Source: Internet
Author: User
Tags tag name tagname
<span id="Label3"></p><p><p>The so-called sax, the Sax simple API for xml, is an easy API for parsing XML Files.</p></p><p><p>The SAX parsing XML works by sequentially scanning the document, and when scanning to the beginning and end of the document, the element starts and ends, triggering the corresponding response Function. is an Event-driven parsing method. It can stop parsing at any point in the parsing document.</p></p><p><p></p></p><p><p>Through this diagram we can understand the process of parsing.</p></p><pre><pre><code>private void parse(String xmlString,List<Mp3Info> infos){ // SAXParserFactory 是一个使用工厂模式的类 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(); }}</code></pre></pre><p><p>Parsing an XML file using sax, first to get a saxparserfactory object, and then get a XmlReader object, Then pass the class object of the ContentHandler interface that you implement to the Setcontenthandler method of xmlreader, and finally call the parse method of xmlreader, and put the XML file generation InputSource that needs parsing.</p></p><p>The key of <p> Parsing XML file is to implement the class of ContentHandler interface, in this class, we can parse the XML file according to our requirement, use and store the content under different tags according to our own NEEDS. </p></p><pre><code>/** * XML parsing class, by inheriting the defualthandler, and copying the methods in the class parsing the XML file * Here is the SAX parsing method, while not choosing to implement the interface, but through the adapt mode, inherit a ContentHandler interface class * @author Ronche * */public class Mp3listcontenthandler extends DefaultHandler {private list<mp3info> infos = null; Private Mp3info Mp3info = null; Private String TagName = null; Public Mp3listcontenthandler (list<mp3info> INFOS) {super (); This.infos = infos; }/* * (non-javadoc) * @see org.xml.sax.helpers.defaulthandler#characters (char[], int, int) * Process the contents of the file */ @Override public void characters (char[] ch, int start, int Length) throws saxexception {String I NFO = 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 a set tag name, implement a specific method if (localname.equals ("resource")) {infos.add (mp3info); } tagname = ""; } @Override public void Startdocument () throws saxexception {} @Override public void startelement (String u ri, string localname, string qName, Attributes Attributes) throws saxexception {//each time new resolves to a label, the tag name is recognized, according to Different label names, call different Methods This.tagname = localname; If (tagname.equals ("resource")) {mp3info = new Mp3info (); } }}</code></pre><p><p>Through this class, the main implementation is to parse a mp3resource.xml file, through the file read out the name of the MP3 file, size, id, and the name of the lyrics LRC file, stored in a list of the List. The following is an XML file:</p></p><pre><pre><code><?xml version="1.0" encoding="ISO-8859-1"?><resources> <resource> <id>0001</id> <mp3.name>a1.mp3</mp3.name> <mp3.size>10033011</mp3.size> <lrc.name>a1.lrc</lrc.name> <lrc.size>9053</lrc.size> </resource> <resource> <id>0002</id> <mp3.name>a2.mp3</mp3.name> <mp3.size>10033011</mp3.size> <lrc.name>a2.lrc</lrc.name> <lrc.size>9053</lrc.size> </resource></resources></code></pre></pre> <p><p>XML parsing of Android</p></p></span>

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.