我想在iOS中读取XML文件。我像这样阅读了
NSURL *url = [[NSURL alloc] initWithString:urlString] ;
purchasedBookXMLParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
PurchasedBookXMLParser *bookParser = [[PurchasedBookXMLParser alloc] initPurchasedBookXMLParser];
[purchasedBookXMLParser setDelegate:bookParser];
BOOL success = [purchasedBookXMLParser parse];
,但我并非总是会取得成功。当我在浏览器中给出相同的URL时,我可以看到XML数据。这样做的原因是什么。我该如何解决这个问题。请帮助我。
谢谢
您是否实现了解析器委托法?在我的项目中,我正在做的是以下
NSURL *url = [NSURL URLWithString:@"https://api.sportsdatallc.org/nascar-p3/sc/2015/races/schedule.xml?api_key=wstdt33unpyk7sgpmjj5qver"];
parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[parser setDelegate:self];
[parser setShouldResolveExternalEntities:NO];
[parser parse];
在那之后,解析器委托方法呼叫,即如下如果您想要" race"标签之间的数据,则[element iSequalToString:@" race"]
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
NSString *element;
element = elementName;
if ([element isEqualToString:@"race"])
{
item2=[[NSMutableDictionary alloc] init];
[item2 setObject:attributeDict forKey:@"race"];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
[self.tableview reloadData];
}