如何从服务器解析XML文件? [英] How to parse XML file from a server?

查看:74
本文介绍了如何从服务器解析XML文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了很多内容,但是这些教程和示例代码都无济于事.我正在尝试解析一个非常简单的XML数据,它将始终是相同的,只有一个结果,该xml是下面的代码:

I've already searched a lot but none of the tutorials and sample codes helped. I am trying to parse a very simple XML data, which will be always the same with just one result, the xml is code bellow:

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

那么,如何使用NSXMLParser或您知道的另一种方式来解析此类文件?

So, how can I parse such a file using NSXMLParser or another way you know?

推荐答案

好吧,我以与众不同的方式解析xml,坦白地说,我真的不知道它是哪种技术,但是我向您保证,它对我来说很好用,我有在许多项目中成功实现了它.看一下我的代码,我从某些配置文件中加载推文

Well I parse xml some different way than others and being frank I really do not know which technique it is but I assure you it works fine for me and I have implemeted it successfully in so many projects. Have a look at my code where I load tweets from some 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委托方法:

And these are the NSURLConnection delegate methods:

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

}

我使用了一些字符串函数来分隔标签,并将值存储在Array中.

I have used some string functions to separate tags and stored the values in Array.

这篇关于如何从服务器解析XML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆