XML parsing in IOS: DOM and SAX, iosxmldomsax

Source: Internet
Author: User

XML parsing in IOS: DOM and SAX, iosxmldomsax

I. Introduction

Dom is a set of standards specified by w3c. The core is to process data in a tree structure. The dom parser reads xml files and creates a tree with the same structure in the memory ", each section of the tree corresponds to each xml tag, and the "Tree" is manipulated to process files in the xml. When the xml file is large, the created "Tree" will also be large, so it will occupy a lot of memory.
The core of the sax Parser is the event processing mechanism. For example, when a parser discovers a starting tag, it encapsulates the detected data into a tag start event and reports the event to the event processor. The event processor then calls the method (startElement) process the discovered data. Next we will try to parse the SAX and DOM:

The XML format is as follows:

Ii. SAX:

1. We use Apple's NSXMLParser for parsing with SAX. First, initialize a parser and set the proxy to start parsing. The Code is as follows:

// Initialization path NSString * path = [[NSBundle mainBundle] pathForResource: @ "Test. xml "ofType: nil]; NSData * data = [NSData dataWithContentsOfFile: path]; // initialize the parser NSXMLParser * parser = [[NSXMLParser alloc] initWithData: data]; // sets the proxy parser. delegate = self; // start parsing [parser parse];

2. Start parsing through the proxy method. Because the sax method is event-driven, parsing is performed in order. To obtain tag data, we need to monitor the parsing status of each step, therefore, the system provides a proxy method. The following are common proxy methods:

// Start to load the document-(void) parserDidStartDocument :( NSXMLParser *) parser {} // end to load the document-(void) parserDidEndDocument :( NSXMLParser *) parser {} // start parsing the tag-(void) parser :( NSXMLParser *) parser didStartElement :( NSString *) elementName namespaceURI :( NSString *) namespaceURI qualifiedName :( NSString *) qName attributes :( NSDictionary *) attributeDict {} // tag resolution end-(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 storage methods: attribute and text between tags. If you want to obtain attributes, you must obtain them in the proxy method,

 

The proxy method is required to obtain the text between tags.

 

Conclusion: by combining the given proxy method, we can easily obtain XML content.

3. DOM

1. We use a third-party framework GDataXMLNode for DOM parsing, which is a dynamic library. We need to introduce the following:

Third-party support frameworks must also be introduced:

DOM Parsing is a little simpler than SAX. DOM first puts the entire file into the memory. Then obtain the element content in the memory in the form of a binary tree.

  Main categories involved:

GDataXMLDocument // the entire document

GDataXMLElement // Node object

GDataXMLNode // attribute Node object

You can use the following method to parse data:

// Initialize // Add the file to the memory GDataXMLDocument * document = [[GDataXMLDocument alloc] initWithData: data options: 0 error: nil]; // obtain the root element GDataXMLElement * root = document. rootElement; // obtain all the subnodes of the name-(NSArray *) elementsForName :( NSString *) name; // obtain the attribute value based on the name-(GDataXMLNode *) attributeForName :( NSString *) name;

In this way, we can easily parse and obtain XMl values,

 

For more information, clickView Source CodeRun the test in person.

For questions or technical exchanges, please join the official QQ group: (452379712)

 

Author: Jerry Education
Source: http://www.cnblogs.com/jerehedu/
The copyright of this article belongs to Yantai Jerry Education Technology Co., Ltd. and the blog Park. You are welcome to repost it. However, you must keep this statement without the author's consent and provide the original article connection on the article page, otherwise, you are entitled to pursue legal liability.

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.