NSXMLParser (SAX parsing) Large File Parsing
NSXMLParser (SAX parsing) Large File Parsing
-(Void) viewDidLoad
{
[
{
[SuperviewDidLoad];
// 1. Create a URL
NSURL * url = HMUrl (@ "video? Type = XML ");
// 2. Create a request
NSURLRequest * request = [NSURLRequestrequestWithURL: url];
// 3. Send the request
[NSURLConnectionsendAsynchronousRequest: requestqueue: [NSOperationQueuemainQueue] completionHandler: ^ (NSURLResponse * response, NSData * data, NSError * connectionError ){
If (connectionError | data = nil ){
[MBProgressHUDshowError: @ "the network is busy. Please try again later! "];
Return;
}
// Parse XML data
// 1. Create an XML Parser -- SAX -- parse elements one by one
NSXMLParser * parser = [[NSXMLParseralloc] initWithData: data];
// 2. Set proxy
Parser. delegate = self;
// 3. Start parsing (synchronous execution)
[Parser parse];
// 4. Refresh the table
[Self. tableViewreloadData];
}];
}
# Proxy method of pragma mark-NSXMLParser
/**
* It is called when it is parsed to the beginning of the document.
*/
-(Void) parserDidStartDocument :( NSXMLParser *) parser
{
// NSLog (@ "parserDidStartDocument ----");
}
/**
* It is called when an element is parsed to the beginning.
*
* @ Param elementName: element name
* @ Param attributeDict attribute dictionary
*/
-(Void) parser :( NSXMLParser *) parser didStartElement :( NSString *) elementName namespaceURI :( NSString *) namespaceURI qualifiedName :( NSString *) qName attributes :( NSDictionary *) attributeDict
{
If ([@ "videos" isEqualToString: elementName]) return;
HMVideo * video = [HMVideovideoWithDict: attributeDict];
[Self. videosaddObject: video];
}
/**
* After an element is parsed, it is called.
*
* @ Param elementName: element name
*/
-(Void) parser :( NSXMLParser *) parser didEndElement :( NSString *) elementName namespaceURI :( NSString *) namespaceURI qualifiedName :( NSString *) qName
{
// NSLog (@ "didEndElement ---- % @", elementName );
}
/**
* It will be called when it is parsed to the end of the document (resolution ends)
*/
-(Void) parserDidEndDocument :( NSXMLParser *) parser
{
// NSLog (@ "parserDidEndDocument ----");
}