Network data parsing for iOS development (2) -- XML parsing introduction, ios -- xml
This article describes XML parsing.
Body:
1. There are two XML parsing methods:
- DOM: loads XML data into memory for parsing at a time, which is suitable for parsing small files.
- SAX: Starting from the root element, one element and one element are parsed in sequence, which is suitable for parsing large files.
2. There are many XML parsing solutions in IOS:
2-1. Third-party framework:
Libxml2: pure C language. It is included in the iOS SDK by default and supports DOM and SAX parsing.
GDataXML: DOM-based parsing, developed by Google, based on libxml2
2-2. Apple native
NSXMLParser: simple to use
2-3. I am using an XML parsing tool encapsulated by the native NSXMLParser of apple.
HYXMLParser: it is simpler and simpler to use than NSXMLParser for parsing in the SAX mode.
: HYXMLParser
3. XML parsing method selection suggestions
Large files: NSXMLParser, HYXMLParser, and libxml2 (but almost none)
Small files: GDataXML, NSXMLParser, HYXMLParser, and libxml2
4. NSXMLParser example:
The parsing process follows three steps:
The above example source code Baidu cloud sharing link: http://pan.baidu.com/s/1c1oLiRu password: byxc
5. AboutMyselfSample Code of HYXMLParser created by encapsulating NSXMLParser:
<Benefit: it is easy to use and can parse XML for any type of model objects. It has high reusability and is highly cohesive and low coupling with the outside world.>
The example shown here is the XML parsing tool class for the 3rd versions of XMLParser_3 downloaded from hybrixmlparser on my github:
5-1. Copy the HYXMLParse file tool class directly to your project:
5-2, and then use:
Based on actual needs, the dictionary-to-model method may be most appropriate to replace KVC with MJExtension.
If you are familiar with the native NSXMLParser of apple, use the encapsulated HYXMLParser again. You will find that the encapsulated HYXMLParser is easy to use.
At the same time, it is recommended that readers who have just learned XML parsing should first master Apple NSXMLParser parsing, that is, the parsing process described at the previous 4th points, and then learn to use their own HYXMLParser.
6. Let's briefly talk about the implementation idea of the encapsulated HYXMLParser_3.
6-1. encapsulate the native NSXMLParser of Apple in a common NSObject class by using the proxy method SAX to parse XML.
6-2. In the proxy parsing method, because Model objects need to be constantly created, here I use the NSObjectFromString method to create class objects by class names, then, create a model object through the class object.
In this way, the Class Name of the model object can be passed in externally through the NSString parameter. At the same time, the class of the model object is decoupled from the model object and does not need to be imported.
6-3. based on actual needs, MJExtension dictionary conversion models may be required to replace the native KVC of apple with limited functions. Therefore, Block is used to provide open interfaces, allows external users to freely select the dictionary-to-model method.
Reprinted with the source: http://www.cnblogs.com/goodboy-heyang/p/5188410.html, respecting the fruits of labor.