XML parsing DOM and sax in iOS

Source: Internet
Author: User

First, Introduction

Dom is a set of normative standards specified by the consortium. The core is to process the data in a tree-shaped structure, and the DOM parser reads the XML file and constructs an identical "tree" in memory, with each node and XML tag corresponding to manipulate the file in the XML by manipulating the "tree". When the XML file is very large. The established "tree" will also be large, so it will occupy a lot of memory.


The core of the SAX parser is the event handling mechanism. For example, when the parser discovers the start tag of a tag. The discovered data is encapsulated as a token start event, and this is reported to the event handler. The event handler re-calls the method (Startelement) to process the discovered data.

Let's try the sax and Dom parsing:

The XML format is as follows:

Second, SAX:

  1, we use SAX parsing with the Apple Nsxmlparser, the first initialization of a parser, by setting up the agent to start parsing, code such as the following:

Initialization path  NSString * path=[[nsbundle Mainbundle] pathforresource:@ "Test.xml" oftype:nil];  NSData * Data=[nsdata Datawithcontentsoffile:path];  Initialize Parser Nsxmlparser * Parser=[[nsxmlparser alloc] initwithdata:data];//set agent parser.delegate=self;//start parsing [parser parse ];

2, through the agent method to start the analysis. Because the SAX mode is event driven, parsing is parsed sequentially, assuming that you want to get the data for the tag we need to monitor the parsing of each step. Therefore, the system provides proxy methods to us, the following is often used proxy method:

Start loading document-(void) Parserdidstartdocument: (Nsxmlparser *) parser{}//End loading document-(void) Parserdidenddocument: (Nsxmlparser *) parser{}//Start parsing label-(void) Parser: (Nsxmlparser *) parser didstartelement: (NSString *) elementname NamespaceURI: (nsstring *) NamespaceURI QualifiedName: (NSString *) QName attributes: (nsdictionary *) attributedict{}//label parsing End-(void) parser: ( Nsxmlparser *) Parser didendelement: (NSString *) elementname NamespaceURI: (NSString *) NamespaceURI QualifiedName: ( NSString *) qname{}//read text between tags-(void) Parser: (Nsxmlparser *) parser foundcharacters: (NSString *) string{content=    string;}

3, XM has two methods of deposit, one is a property, and one is the text between the tags. Suppose we want to get properties that need to be obtained in the proxy method.

Suppose it's a proxy method to get the text between tags

Summary : With the combination of a given proxy approach we can easily get the XML content

Third, DOM

1, we use the DOM parsing with the third-party framework Gdataxmlnode, the framework is a dynamic library. We need to introduce such as the following:

A third-party support framework is required at the same time:

Dom parsing is slightly simpler relative to sax, where the DOM first puts the entire file into memory. The contents of the elements are then retrieved in memory as a two-fork tree.

  The main classes involved are:

Gdataxmldocument//Entire document

Gdataxmlelement//Node object

Gdataxmlnode//Attribute Node object

We can parse the data in the following ways

Initialize//Add file to memory Gdataxmldocument * Document=[[gdataxmldocument alloc] initwithdata:data options:0 Error:nil];  Get root element    gdataxmlelement  * root=document.rootelement;//get all child nodes of that name-(Nsarray *) Elementsforname: (NSString *) name;//gets the attribute value based on the name-(gdataxmlnode*) Attributeforname: (NSString *) name;

This allows us to easily parse and get the value of the XML.

Small partners who want to know a lot of other content can click to view the source code and perform the test in person.

Inquiries or technical exchanges, please add the official QQ Group: (452379712)

Jerry Education
Source:http://blog.csdn.net/jerehedu/
This article belongs to Yantai Jerry Education Technology Co., Ltd. and CSDN co-owned. Welcome reprint. However, this statement must be retained without the author's permission. And in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.

XML parsing DOM and sax in iOS

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.