开发者

XML attribute pasrsing problem

i have to parse an xml and the link of that xml is

http://qasimshah.sitesled.com/schedule.xml

and may to parse is:

-(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 = [开发者_JAVA技巧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);
    }

}

but when i try to get 'id' or name parser detect it but do not return me its value. so how can i get its value?


You should get the attribute value in "attributeDict"

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{            

you are only referring element ...


Try this. You can get all the values like this.

        - (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);
            }
        }


In didStartElement: you're checking for "Sports" but in didEndElement: you're checking for "Sport"...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜