XML parsing DOM and sax in iOS

Source: Internet
Author: User

First, Introduction

Dom is a set of specifications specified by the list, the core is to process the data in a tree structure, the DOM parser reads the XML file and constructs an identical "tree" in memory, the tree nodes and XML tags correspond to manipulate the "tree" to work with the files in the XML. When the XML file is large, the established "tree" is also large, so it takes up 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, which is reported to the event handler, and the event handler re-call method (Startelement) processes the discovered data. Let's try sax and Dom parsing:

The XML format is as follows:

Second, SAX:

  1, we use SAX parsing with the Apple Nsxmlparser, first initialize a parser, by setting up the agent to start parsing, the code is as follows:

 //  initialize path  NSString * path=[[nsbundle Mainbundle] Pathforresource:@ " test.xml     Oftype:nil];  NSData  * Data=[nsdata Datawithcontentsoffile:path];  //  initialize parser  Nsxmlparser * parser=< Span style= "color: #000000;" >[[nsxmlparser alloc] initwithdata:data];  //  Set the proxy  parser. delegate  =self;  //  start parsing  [parser parse]; 

2, through the proxy method to start parsing, because the SAX mode is event-driven, so parsing is in order to parse, if you want to get the label data we need to monitor the parsing of each step, so the system provides proxy method to us, the following is the common proxy method:

//Start loading document- (void) Parserdidstartdocument: (Nsxmlparser *) parser{}//End Loading Document- (void) Parserdidenddocument: (Nsxmlparser *) parser{}//Start parsing Labels- (void) Parser: (Nsxmlparser *) parser didstartelement: (NSString *) elementname NamespaceURI: (NSString *) NamespaceURI QualifiedName: (NSString *) QName attributes: (nsdictionary *) attributedict{}//end of label parsing- (void) Parser: (Nsxmlparser *) parser didendelement: (NSString *) elementname NamespaceURI: (NSString *) NamespaceURI QualifiedName: (NSString *) qname{}//read the 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. If we want to get properties that need to be obtained in the proxy method,

If you are getting the text between tags, you need to use the proxy method

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

Third, DOM

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

There is also a need to introduce a third-party support framework:

Dom parsing is a little simpler than 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

//Add files to memory gdataxmldocument * Document=[[gdataxmldocument alloc] initwithdata:   Data options:0  Error:nil];   // Get root element    Gdataxmlelement  * root=document.rootelement; // gets all child nodes of the name -(Nsarray *) Elementsforname: (NSString *) name; // gets the property value by name -(gdataxmlnode*) Attributeforname: (NSString *) name;

So that we can easily parse and get the value of the XML,

To learn more about the small partners, you can click to view the source code , run the test yourself.

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

Jerry Education
Source:http://www.cnblogs.com/jerehedu/
This article is the copyright of Yantai Jerry Education Technology Co., Ltd. and the blog Park is shared, welcome reprint, but without the consent of the author must retain this paragraph statement, 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.