我正在处理1个应用程序,该应用程序需要多次调用Web服务器以检索所有数据,因为每个请求的限制为25个项目。如何使用XMLParser做到这一点?
在“ startParsing”方法中,我们使用URL初始化NSXMParser并设置委托。由于服务器限制,我们如何多次调用XML解析器或多次从服务器请求数据?我们需要更改URL还是什么?
-(void)startParsing{
NSXMLParser *xmlparser = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://musicbrainz.org/ws/2/place/?query=placeAND%20type:StudioANDbegin:[1990%20TO%202018]"]];
[xmlparser setDelegate:self];
[xmlparser parse];
if (_brainzXMLData.count != 0) {
}
}
#pragma mark - NSXML Delegate methods
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict;
{
//Retrieving metadata and place objects
if ([elementName isEqualToString:@"metadata"]) {
_brainzXMLData = [[NSMutableArray alloc] init];
}
if ([elementName isEqualToString:@"place"]) {
_brainzDictXMLPart = [[NSMutableDictionary alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string;
{
if (!_brainzXMLString) {
_brainzXMLString = [[NSMutableString alloc] initWithString:string];
}
else {
[_brainzXMLString appendString:string];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName;
{
//Here we retrieve useful information and save it in 'brainzXMLData' dictionary.
if ([elementName isEqualToString:@"name"]) { //If elementName is name, set object and dictionary here.
[_brainzDictXMLPart setObject:_brainzXMLString forKey:elementName];
}
if ([elementName isEqualToString:@"begin"]) { //If elementName is begin, set object and dictionary here.
[_brainzDictXMLPart setObject:_brainzXMLString forKey:elementName];
}
if ([elementName isEqualToString:@"end"]) { //If elementName is end, set object and dictionary here.
[_brainzDictXMLPart setObject:_brainzXMLString forKey:elementName];
}
if ([elementName isEqualToString:@"place"]) { // Add all data into brainzXMLData dictionary.
[_brainzXMLData addObject:_brainzDictXMLPart];
}
_brainzXMLString = nil;
}