|
/**
* 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");
}
|