How to parse complex xml files in android
This article mainly explains how to parse XML files in Android development. Since XML files are platform-independent and widely used in data communications, parsing XML files is very meaningful. There are three main methods for Android to parse XML files. There are usually three methods: DOM, SAX and PULL. These three methods will be discussed below.
The content of the file is as follows:
Then the XML file needs to be parsed. Next, we will use DOM, SAX and PULL to parse this XML file respectively.
DOM method
The DOM method of parsing xml is to first read the xml document into the memory, and then use the DOM API to access the tree structure and obtain the data. It can be known from the DOM parsing method that if the XML file is large, the processing efficiency will become relatively low, which is also a shortcoming of the DOM method.
Now let’s parse the xml file related to the weather forecast information mentioned above. What is parsing? To put it more simply, it means to identify this tagged XML file and extract some relevant and useful information for us to use. In this file, the time, weather, temperature, and icons are what we need to get. We need to analyze it.
The specific idea of parsing is:
1. Load the XML file.
2. Get the root node of the document
3. Get the list of all child nodes in the document root node
4. Get the list of child nodes that need to be read Node information
Based on these 4 steps, we develop:
The first is how to load the XML file, assuming that this file comes from the network.
SAX method
SAX is the abbreviation of Simple API for XML. It is a package and can also be regarded as some interfaces.
Compared with DOM, SAX is a faster, more efficient, and less memory-consuming method of parsing XML files. It is a progressive scan and can be parsed while scanning, so SAX can stop parsing at any time while parsing the document. Great for mobile devices like Android.
SAX is event-driven. The so-called event-driven means that it does not need to parse the entire document. During the process of parsing the document in content order, SAX will determine whether the characters currently read conform to a certain part of the XML file syntax. If a certain part is met, an event is triggered. The so-called triggering event is to call some callback methods. Of course, Android's event mechanism is based on the callback method. When using SAX to parse an XML document, an event will be called back when the start and end tags of the document are read, and an event will be called back when other nodes and content are read. In the SAX interface, the event source is the XMLReader in the org.xml.sax package, which parses the XML document through the parser() method and generates events. The event handler is the four interfaces ContentHander, DTDHander, ErrorHandler, and EntityResolver in the org.xml.sax package.
The detailed description of these four interfaces is as follows:
Event handler name
Events processed by the event handler
XMLReader registration method
ContentHander
The beginning and end of the XML document,
The beginning and end of the XML document tag, receiving character data, skipping entities, and receiving elements that can be Ignore whitespace etc.
setContentHandler(ContentHandler h)
DTDHander
Handle the corresponding events generated during DTD parsing
setDTDHandler(DTDHandler h)
ErrorHandler
Errors generated when processing XML documents
setErrorHandler(ErrorHandler h)
EntityResolver
Processing external entities< /p>
setEntityResolver(EntityResolver e)
The callback methods we use for content parsing are generally defined in the ContentHandler interface.
Commonly used methods of the ContentHandler interface:
startDocument()
When you encounter the beginning of the document, call this method and do some preprocessing in it work.
endDocument()
When the document ends, call this method to do some aftermath work.
startElement(String namespaceURI, String localName,String qName, Attributes atts)
This method will be called when the start tag is read. namespaceURI is the namespace, localName is the label name without the namespace prefix, and qName is the label name with the namespace prefix. All attribute names and corresponding values can be obtained through atts.
endElement(String uri, String localName, String name)
This method is called when an end tag is encountered.
characters(char[] ch, int start, int length)
This method is used to process the content read in the XML file. For example:
The first parameter is used to store the content of the file. The next two parameters are the starting position and length of the read string in this array. Use new String(ch,start,length) to Content is available.
Note:
An important feature of SAX is its streaming processing. When a tag is encountered, it will not record the tags encountered before, that is, In the startElement() method, all the information that can be known is the name and attributes of the tag. As for the nested structure of the tag, the name of the upper tag, whether there are sub-element attributes, and other structure-related information, we do not know. , all require your program to complete. This makes SAX less convenient than DOM in programming.
Now we intercept an XML file for parsing. The calling method is as follows:
--------- -> startDocument()
beijing ----------> characters city> ----------> endElement
-- --------> endElement
End of document ----------> endDocument()
SAX parsing steps:
< p>The first thing to note is:SAX has also developed a Helper class for it: DefaultHandler, which implements the ContentHandler interface, but all its method bodies are empty. When implementing it, you only You need to inherit this class and then overload the corresponding methods.
Using SAX to parse XML files generally has the following five steps:
1. Create a SAXParserFactory object;
2. Call the newSAXParser method in SAXParserFactory to create a SAXParser object;
3. Then call the getXMLReader method in SAXParser to obtain an XMLReader object;
4. Instantiate a DefaultHandler object
5. Connection event Transfer the source object XMLReader to the event processing class DefaultHandler
6. Call the parse method of XMLReader to obtain the xml data from the input source
7. Return the data collection we need through DefaultHandler.
We are still going to parse the XML file of the weather forecast mentioned above.