Two common methods of "iOS development-Network" parsing XML data

Source: Internet
Author: User

There are two common ways to parse XML data

First Kind

Parsing XML data using Google's Gdataxmlnode
Dom mode parsing is used.
First swallow the XML and then the 1.1-point resolution

The second Kind

Parsing XML data using Apple's own Nsxmlparser
Using the Sax parsing method
Parsing of a tag for a token

First step of Use step

First put the Gdataxml folder into the project

Step Two

Change what's inside bulid setting.

Change header File Search path

Add a path inside the header Search paths/USR/INCLUDE/LIBXML2

Add-LXML2 inside the other Linker flags

Change the GDATAXMLNODE.M file to a non-arc environment

The third step introduces the header file
#import "GDataXML/GDataXMLNode.h"
Code
Load the entire document gdataxmldocument *doc = [[Gdataxmldocument alloc] initwithdata:data options:0Error:nil];Get with elemental gdataxmlelement *rootelement = doc. RootElement;Get all the video elements Nsarray *elements= [rootelement elementsforname:@"Video"];Iterating over the array for (Gdataxmlelement *eleminchelements) {Video *video = [[Video alloc] init];Video. ID= [Elem attributeforname:@"id"]. StringValue;Video. Length= [Elem attributeforname:@"Length"]. StringValue;Video. Name= [Elem attributeforname:@"Name"]. StringValue;Video. Image= [Elem attributeforname:@"Image"]. StringValue;Video. URL= [Elem attributeforname:@"url"]. StringValue;[Self. VideosAddobject:video];}
The second way to use parsing

This method is provided by Apple and requires the proxy method to be set

The proxy method is invoked whenever a tag is encountered

//创建解析器NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];//设置解析器的代理parser.delegateself;//开始解析[parser parse];//这是一个同步操作

Proxy method

/** * will be called when parsing to the beginning of the document * /- (void) Parserdidstartdocument: (Nsxmlparser *) parser{}/** * Parse to the beginning of an element will call * * @param elementname element name * @param attributedict Property Dictionary */- (void) Parser: (Nsxmlparser *) parser didstartelement: (NSString *) elementname NamespaceURI: (NSString *) NamespaceURI QualifiedName: (NSString *) QName attributes: (nsdictionary *) attributedict{if([@"Videos"Isequaltostring:elementname])return;    Hmvideo *video = [Hmvideo videowithdict:attributedict]; [Self.videos Addobject:video];}/** * Parse to the end of an element will call * * @param elementname element name */- (void) Parser: (Nsxmlparser *) parser didendelement: (NSString *) elementname NamespaceURI: (NSString *) NamespaceURI QualifiedName: (NSString *) qname{}/** * will be called when parsing to the end of the document (parse end) */- (void) Parserdidenddocument: (Nsxmlparser *) parser{}

Two common methods of "iOS development-Network" parsing XML data

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.