我必须解析xml并且该xml的链接是
http://qasimshah.sitesled.com/schedule.xml
并且可能解析是:
-(IBAction)autoLoginBtn{
NSString *url=[NSString stringWithFormat: @"http://qasimshah.sitesled.com/schedule.xml"];
[self parseXMLFileAtURL:url];
}
- (void)parseXMLFileAtURL:(NSString *)URL
{
xmlDataArray1 = [[NSMutableArray alloc] init];
//you must then convert the path to a proper NSURL or it won't work
NSURL *xmlURL = [NSURL URLWithString:URL];
// here, for some reason you have to use NSClassFromString when trying to alloc NSXMLParser, otherwise you will get an object not found error
// this may be necessary only for the toolchain
NSXMLParser *rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
// Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
[rssParser setDelegate:self];
// Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
[rssParser setShouldProcessNamespaces:NO];
[rssParser setShouldReportNamespacePrefixes:NO];
[rssParser setShouldResolveExternalEntities:NO];
[rssParser parse];
}
-(void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError{
NSLog(@"Error on XML Parse: %@", [parseError localizedDescription] );
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
//NSLog(@"found this element: %@", elementName);
currentElement = [elementName copy];
if ([elementName isEqualToString:@"Sports"]) {
// clear out our story item caches...
item = [[NSMutableDictionary alloc] init];
currentTitle = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:@"Sport"]) {
[item setObject:currentTitle forKey:@"id"];
[xmlDataArray1 addObject:[item copy]];
NSLog(@"id: %@", currentTitle);
}
}
但是当我尝试获取'id'或name
解析器检测到它但没有返回它的值。
那我怎么能得到它的价值呢?
答案 0 :(得分:2)
您应该在“attributeDict”中获取属性值
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
你只是指元素......
答案 1 :(得分:1)
试试这个。你可以得到这样的所有价值。
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict
{
if([elementName isEqualToString:@"Sport"])
{
NSString *s = [NSString stringWithFormat:@"%@",elementName];
NSLog(@"%@",s);
NSString *s1 =[NSString stringWithFormat:@"%@",[attributeDict valueForKey:@"id"]];
NSLog(@"%@",s1);
NSString *s2 =[NSString stringWithFormat:@"%@",[attributeDict valueForKey:@"name"]];
NSLog(@"%@",s2);
NSString *s3 =[NSString stringWithFormat:@"%@",[attributeDict valueForKey:@"abbr"]];
NSLog(@"%@",s3);
}
}
答案 2 :(得分:1)
在didStartElement:
你正在检查“体育”,但在didEndElement:
你正在检查“体育”......