Network data parsing for iOS development (2) -- XML parsing introduction, ios -- xml

Source: Internet
Author: User

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.

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.