iOS development uses nsxmlparser, gdataxml two ways to parse XML data

Source: Internet
Author: User

Summary: XML data parsing method has

*ios native Nsxmlparser, only supports sax parsing, easy to use

* C language LIBXML2, while supporting DOM and sax parsing

*google Gdataxml, only support DOM parsing, based on LIBXML2, need to configure the compilation environment as non-arc, and add libxml2.dylib dynamic Library

Gdataxmldocument

1 parsing XML using Gdataxml's Dom method requires importing #import "GDataXMLNode.h"

/**

* Dom method parsing XML data

* Pre-knowledge of XML data structure

*/

-(void) analysisxmlbydom{

Get URL

Nsurl *url = [[Nsurl alloc] initwithstring:@ "Http://localhost:8070/MJServer/video?type=XML"];

Get an instance of session

Nsurlsession *session = [Nsurlsession sharedsession];

Create a data Access task

Nsurlsessiondatatask *task = [Session Datataskwithurl:url completionhandler:^ (NSData *data, Nsurlresponse *response, Nserror *error) {

If an access error

if (Error | | data = nil) {

NSLog (@ "network busy, please try again later");

Return

}

Load the entire XML data

Gdataxmldocument *doc = [[Gdataxmldocument alloc] Initwithdata:data options:0 Error:nil];

Gets the root element of the XML data

Gdataxmlelement *root = [Doc rootelement];

Remove all video node elements under the root element

Nsarray *videos = [Root elementsforname:@ "video"];

Iterating through an array to fetch data

For (gdataxmlelement *element in videos) {

NSLog (@ "id-%@", [element attributeforname:@ "id"].stringvalue);

NSLog (@ "name-%@", [element attributeforname:@ "name"].stringvalue);

NSLog (@ "url-%@", [element attributeforname:@ "url"].stringvalue);

printf ("\ n----------------gorgeous split line-----------------\ n");

}

}];

[Task resume];

}

2 parsing XML using Nsxmlparser's Sax method

/**

* Dom method parsing XML data

* Pre-knowledge of XML data structure

*/

-(void) analysisxmlbydom{

Get URL

Nsurl *url = [[Nsurl alloc] initwithstring:@ "Http://localhost:8070/MJServer/video?type=XML"];

Get an instance of session

Nsurlsession *session = [Nsurlsession sharedsession];

Create a data Access task

Nsurlsessiondatatask *task = [Session Datataskwithurl:url completionhandler:^ (NSData *data, Nsurlresponse *response, Nserror *error) {

If an access error

if (Error | | data = nil) {

NSLog (@ "network busy, please try again later");

Return

}

Load the entire XML data

Gdataxmldocument *doc = [[Gdataxmldocument alloc] Initwithdata:data options:0 Error:nil];

Gets the root element of the XML data

Gdataxmlelement *root = [Doc rootelement];

Remove all video node elements under the root element

Nsarray *videos = [Root elementsforname:@ "video"];

Iterating through an array to fetch data

For (gdataxmlelement *element in videos) {

NSLog (@ "id-%@", [element attributeforname:@ "id"].stringvalue);

NSLog (@ "name-%@", [element attributeforname:@ "name"].stringvalue);

NSLog (@ "url-%@", [element attributeforname:@ "url"].stringvalue);

printf ("\ n----------------gorgeous split line-----------------\ n");

}

}];

[Task resume];

}

/**

* Sax parsing of XML data

* Pre-knowledge of XML data structure

*/

-(void) analysisxmlbysax{

Get URL

Nsurl *url = [[Nsurl alloc] initwithstring:@ "Http://localhost:8070/MJServer/video?type=XML"];

Get an instance of session

Nsurlsession *session = [Nsurlsession sharedsession];

Create a data Access task

Nsurlsessiondatatask *task = [Session Datataskwithurl:url completionhandler:^ (NSData *data, Nsurlresponse *response, Nserror *error) {

If an access error

if (Error | | data = nil) {

NSLog (@ "network busy, please try again later");

Return

}

Creating a parser

Nsxmlparser *parser = [[Nsxmlparser alloc] initwithdata:data];

Set the agent for the parser to be the current controller

Parser.delegate = self;

Start parsing

[Parser parse];

}];

[Task resume];

}

Proxy methods for #pragma mark-nsxmlparserdelegate

Parsing to the beginning of the document

-(void) Parserdidstartdocument: (Nsxmlparser *) parser{

NSLog (@ "parserdidstartdocument");

}

Parse to element start

-(void) Parser: (Nsxmlparser *) parser didstartelement: (NSString *) elementname NamespaceURI: (NSString *) NamespaceURI QualifiedName: (NSString *) QName attributes: (nsdictionary *) attributedict{

NSLog (@ "didstartelement");

Parsing to the video element

if ([@ "video" Isequaltostring:elementname]) {

NSLog (@ "id-%@", attributedict[@ "id"]);

NSLog (@ "name-%@", attributedict[@ "name"]);

NSLog (@ "url-%@", attributedict[@ "url"]);

printf ("\ n----------------gorgeous split line-----------------\ n");

}

}

Parse to end of element

-(void) Parser: (Nsxmlparser *) parser didendelement: (NSString *) elementname NamespaceURI: (NSString *) NamespaceURI QualifiedName: (NSString *) qname{

NSLog (@ "didendelement");

}

Parse to end of document

-(void) Parserdidenddocument: (Nsxmlparser *) parser{

NSLog (@ "parserdidenddocument");

}

iOS development uses nsxmlparser, gdataxml two ways to parse 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.