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