开发者

XMLParser Iphone Device Problem

i have developed an applikation which loads a specified url with an XML Parser. This URL changes sometimes, and every 4 seconds it reloads.(via NSTIMER)

On the simulator it works perfectly but on the device it is always the same ( like a kind of caching)

-(void)parseXMLFileAtURL:(NSString *) URL{
    NSLog(@"Parsed XML URL: %@", URL);
    currentURL=[URL copy];;
    tutorials=  [[NSMutableArray alloc] init];
    settings=   [[NSMutableArray alloc] init];
    NSURL *xmlURL=[NSURL URLWithString:URL];
    xmlParser =[[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
    [xmlParser setDelegate:self];
    [xmlParser parse];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(开发者_StackOverflowNSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict{
currentElement= [elementName copy];
    if([elementName isEqualToString:@"item"]){
       item=[[NSMutableDictionary alloc] init];
        currentTitle=[[NSMutableString alloc] init];
        currentImage=[[NSMutableString alloc] init];
        currentLink=[[NSMutableString alloc] init];
        currentDetails=[[NSMutableString alloc] init];
        }
    if([elementName isEqualToString:@"setting"]){
        setting=[[NSMutableDictionary alloc] init];
        currentTitle=[[NSMutableString alloc] init];
        currentTyp=[[NSMutableString alloc] init];
        currentLink=[[NSMutableString alloc] init];
    }

}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if([elementName isEqualToString:@"item"]){
    NSLog(@"currenttitle: %@",currentTitle );
        [item setObject:currentTitle forKey:@"title"];
        [item setObject:currentImage forKey:@"image"];      
        [item setObject:currentLink forKey:@"link"];
        [item setObject:currentDetails forKey:@"details"];
        [tutorials addObject:[item copy]];
    }
    if([elementName isEqualToString:@"setting"]){
        NSLog(@"Currentsettingtitle: %@",currentTitle );
        [setting setObject:currentTitle forKey:@"title"];
        [setting setObject:currentTyp forKey:@"typ"];       
        [setting setObject:currentLink forKey:@"link"];
        [settings addObject:[setting copy]];
    }
}


}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if([currentElement isEqualToString:@"title"]){
        [currentTitle appendString:string];
    }
    if([currentElement isEqualToString:@"image"]){
        [currentImage appendString:string];
    }   
    if([currentElement isEqualToString:@"link"]){
        [currentLink appendString:string];
    }

    if([currentElement isEqualToString:@"typ"]){
        [currentTyp appendString:string];
    }
    if([currentElement isEqualToString:@"details"]){
        [currentDetails appendString:string];
    }

}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
    NSLog(@"Fertig mit Document");  
    for (int i=0; i<[settings count]; i++) {
        NSString *typ =(NSString*)[[settings objectAtIndex:i ] objectForKey:@"typ"];
        typ=[typ stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
        if([typ isEqualToString:@"Button1"])
            button1.title=(NSString*)[[settings objectAtIndex:i ] objectForKey:@"title"];
        if([typ isEqualToString:@"Button2"])
            button2.title=(NSString*)[[settings objectAtIndex:i ] objectForKey:@"title"];
        if([typ isEqualToString:@"Button3"])
            button3.title=(NSString*)[[settings objectAtIndex:i ] objectForKey:@"title"];
        if([typ isEqualToString:@"Titel"])
        navitem.title=(NSString*)[[settings objectAtIndex:i ] objectForKey:@"title"];
    }
    //[[tutorials ] objectForKey:@"title"]
    [myTableView reloadData];
}   

Anybody an Idea?


You should try to init your parser with data.

And get your data with NSURLRequest and NSURLConnection :

NSURL *xmlURL=[NSURL URLWithString:URL];
NSURLRequest *request = [NSURLRequest requestWithURL:xmlURL cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:0.0f];
xmlParser =[[NSXMLParser alloc] initWithData:[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//etc.


Thats a handful of code (can't avoid with xml parsing)

But, it seems from your question that there is no problem with xml parsing.

So instead, focus on the Timer related code? Perhaps add some logs when it actually gets a new URL?? Just to be sure you are getting a new request???

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜