How to parse XML file from a server?

2019-05-29 12:54发布

问题:

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>

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

回答1:

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

This is the function where I make call for parser.

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

}

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

}

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