如何从服务器解析XML文件?

问题描述:

我已经搜查了很多,但没有一个教程和示例代码的帮助。我试图解析一个非常简单的XML数据,这将是永远只有一个结果相同,将XML代码波纹管:如何从服务器解析XML文件?

<Books> 
    <Book id="1"> 
     <title>USERS ALREADY EXISTS</title> 
    </Book> 
</Books> 

所以,我怎么能使用NSXMLParser或其他方式,您解析这样的文件知道?

+0

[你有什么尝试?](http://whathaveyoutried.com)苹果公司用于解析XML的API [详细记录](https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual /XMLParsing/XMLParsing.html),并具有说明性[示例代码](https://developer.apple.com/library/ios/samplecode/SeismicXML/)。这些不会告诉你到底如何解决你的问题你,但他们应该帮助您熟悉API不够,你可以从那里找到自己的方式,或者至少提出更具体的问题。 – rickster 2012-03-17 05:11:11

+0

您可以通过Ishu的链接也行。我的方法使用字符串函数并且易于理解。采取任何你觉得容易。但是你在实施之前就已经理解了它。祝你好运。 – 2012-03-17 05:31:44

嗯,我解析XML比其他一些不同的方式,并坦白我真的不知道它是技术,但我向你保证,它工作正常,我和我已经在很多项目顺利Implemeted一个吧。看看我的代码,我从一些profile

此负载鸣叫是我作出呼吁分析器功能。

-(void)loadtweet 
{ 
@try 
{ 
    NSString *urlString = [NSString stringWithFormat:@"https://api.twitter.com/1/statuses/user_timeline.xml?screen_name=SrBachchan&count=5"]; 

    NSLog(@"fetching data from--------> : %@",urlString); 

    NSString* escapedUrlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; 

    NSMutableURLRequest *request1 = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:escapedUrlString]]; 

    NSURLConnection *con=[[NSURLConnection alloc] initWithRequest:request1 delegate:self]; 
    if(con) 
     truckData=[[NSMutableData data]retain]; 
} 

@catch (NSException *exception) 
{ 
    UIAlertView *v = [[UIAlertView alloc] initWithTitle:@"ERROR" message:@"Please Try Again Later." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [v show]; 
    [v release]; 
} 

} 

而这些都是NSURLConnection委托方法:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
[truckData setLength:0]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
[truckData appendData:data]; 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 

} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 

{ 
    [tweets removeAllObjects]; 
@try 
{ 
    // [app.trucks removeAllObjects]; 
    NSString *thexml=[[NSString alloc] initWithBytes:[truckData mutableBytes] length:[truckData length] encoding:NSUTF8StringEncoding]; 

    NSArray *array=[thexml componentsSeparatedByString:@"<status>"]; 
    NSLog(@"%d",[array count]); 

    for(int i=1;i<[array count];i++) 
    { 
     NSString *str=[array objectAtIndex:i]; 
     NSArray *arr1=[str componentsSeparatedByString:@"<text>"]; 
     NSString *data=[arr1 objectAtIndex:1]; 
     NSRange ranfrom=[data rangeOfString:@"</text>"]; 
     // nt.truckName=[data substringToIndex:ranfrom.location]; 
     [tweets addObject:[data substringToIndex:ranfrom.location]]; 
    } 
} 

@catch (NSException *exception) 
{ 
    UIAlertView *v = [[UIAlertView alloc] initWithTitle:@"ERROR" message:@"Please Try Again Later." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [v show]; 
    [v release]; 
} 

} 

我已经使用了一些字符串函数分开标签并存储在排列值。

+0

只是为了检查,在你的代码,我想truckData是NSMutableArray的?什么是微博,一个NSArray中呢? – Mateus 2012-03-17 22:21:01

+0

@MateusNunes:是的。推文是NSMutableArray这将持有所有的推文,你会在UITableView中显示它们。尝试把NSLog和输出,你会知道如何使用这些字符串函数.. :-))我会编辑我的答案,并提出一些意见.. :-) – 2012-03-18 06:09:21