我想通過XML解析獲取Google新聞數據並將其保存到數組中。該XML是這樣如何通過XML解析從Google新聞獲取數據
http://news.google.co.in/news?pz=1&cf=all&ned=in&hl=en&output=rss
我想通過XML解析獲取Google新聞數據並將其保存到數組中。該XML是這樣如何通過XML解析從Google新聞獲取數據
http://news.google.co.in/news?pz=1&cf=all&ned=in&hl=en&output=rss
那麼我解析xml比別人不同的方式和坦率我真的不知道它是哪種技術,但我向你保證它對我來說工作正常,我已經在許多項目中成功實現它。看看我的代碼,我從一些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];
}
}
我已經使用了一些字符串函數分開標籤並存儲在排列值。
如果您有任何疑問,請讓我知道。 – 2012-02-24 04:57:54
在這裏,您可以開始:NSXMLParser類引用。
檢查這些線程http://stackoverflow.com/questions/3488409/objective-c-library-for-rss-feed-parsing – 2012-02-22 07:08:53
您是否嘗試搜索XML解析?顯示一些努力。每個人都會幫助你。 – 2012-02-22 09:52:59