Gdataxmlnode Parsing XML data

Source: Internet
Author: User

Parsing XML usually has two ways, DOM and SAX:
When the DOM parses the XML, it reads into the entire XML document and constructs a tree structure (node tree) that resides in memory, which can be retrieved from any XML node by traversing the tree structure to read its properties and values. And, in general, you can query XML nodes directly with the help of XPath.
Sax parsing XML, is based on the event notification mode, while reading the XML document side processing, do not have to wait for the entire document to be loaded before taking action, when the read parsing process encountered the need to handle the object, will be issued a notification to handle it.



1. Introducing Lib File: Libxml2.dylib
2. To add an include step to a project: XCode4.3 Click Project, enter head in the search box in build setting in targets, locate the header search Paths and add/USR/INCLUDE/LIBXML2
3. Copy GDataXMLNode.h, GDATAXMLNODE.M files to the project
4. Add header file GDataXMLNode.h Plus < nsurlconnectiondatadelegate, nsurlconnectiondelegate>
5. In the Viewdidload method://Save the requested data Object Self.mutablearray=[[nsmutablearray Alloc]init];
NSString *[email protected] "url with http"; Nsurlrequest *request=[[nsurlrequest Alloc]initwithurl:url Cachepolicy:nsurlrequestuseprotocolcachepolicy TIMEOUTINTERVAL:15];
[Nsurlconnection connectionwithrequest:request delegate:self];
[Request release];

6. -(void) connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) response{
Nsmutabledata *data=[[nsmutabledata Alloc]init];
Self.receiveddata=data;
}
-(void) connection: (Nsurlconnection *) connection didreceivedata: (NSData *) data{
[Self.receiveddata Appenddata:data];
}
-(void) connectiondidfinishloading: (nsurlconnection *) connection{
Gdataxmldocument *document=[[gdataxmldocument alloc]initwithdata:self.receiveddata options:0 Error:nil]; Gdataxmlelement *root=[document RootElement];
Nsarray *array=[root elementsforname:@ "News"];
For (gdataxmlelement *element in array) {
Gdataxmlelement *title1=[element elementsforname:@ "title"].lastobject;
Gdataxmlelement *time1=[element elementsforname:@ "Time"].lastobject;
Gdataxmlelement *id1=[element elementsforname:@ "id"].lastobject;
News *news=[[news Alloc]init];
News.title=[title1 StringValue];
News.time=[time1 StringValue];
News.newsid=[id1 StringValue];
[Self.mutablearray Addobject:news];
}
}

Gdataxmlnode Parsing XML data

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.