Question

I'm looking for the most "easy-to-implement" method to parse XMLs in Objective-C mobile applications. I tried to use TBXML but I'm a newbie and I got several errors with that...do you think there's something easier out there? Thanks

Was it helpful?

Solution

this is very simple xml parsing..

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title=@"Feeds";
    titarry=[[NSMutableArray alloc] init];
    linkarray=[[NSMutableArray alloc] init];
    NSString *rssaddr=@"http://news.prlog.org/rss.xml";
    NSURL *url=[NSURL URLWithString:rssaddr];
    xmlparser =[[NSXMLParser alloc] initWithContentsOfURL:url];
    [xmlparser setDelegate:self];
    [xmlparser parse];


    // Do any additional setup after loading the view from its nib.
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict;
{

    classelement=elementName;

    if([elementName isEqualToString:@"item"])
    {
        itemselected=YES;
        mutttitle=[[NSMutableString alloc] init];
        mutstrlink=[[NSMutableString alloc] init];
    }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName;
{
    if([elementName isEqualToString:@"item"])
    {
        itemselected=NO;

        [titarry addObject:mutttitle];
        [linkarray addObject:mutstrlink];

    }

}


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string;
{
    if (itemselected)
    {
        if ([classelement isEqualToString:@"title"])
        {
            [mutttitle appendString:string];
        }
        else if ([classelement isEqualToString:@"link"])
        {
            [mutstrlink appendString:string];
        }
    }
}



- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError;
{
    UIAlertView *alt=[[UIAlertView alloc] initWithTitle:@"RSS Reader"
                                                message:[NSString stringWithFormat:@"%@",parseError]
                                               delegate:nil
                                      cancelButtonTitle:@"Close"
                                      otherButtonTitles:nil];

    [alt show];


}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [titarry count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
    }

    cell.textLabel.text=[titarry objectAtIndex:indexPath.row];
    cell.accessoryType=UITableViewCellSelectionStyleBlue;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


    secondViewController *second = [[secondViewController alloc] initWithNibName:@"secondViewController" bundle:nil];
    [self.navigationController pushViewController:second animated:YES];
    NSURL *url=[NSURL URLWithString:[titarry objectAtIndex:indexPath.row]];
    NSURLRequest *req=[NSURLRequest requestWithURL:url];
    second.webView.scalesPageToFit=YES;
    [second.webView loadRequest:req];//here we have to perform changes try to do some things here


}

add following this in your .h file

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,NSXMLParserDelegate>
{
    NSXMLParser *xmlparser;

    NSString *classelement;
    NSMutableArray *titarry;
    NSMutableArray *linkarray;
    bool itemselected;
    NSMutableString *mutttitle;
    NSMutableString *mutstrlink;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;

OTHER TIPS

- (void)applicationDidFinishLaunching:(UIApplication *)application {


    NSURL *url = [[NSURL alloc] initWithString:@"Your link of the XML"];
    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];

    //Initialize the delegate.
    XMLParser *parser = [[XMLParser alloc] initXMLParser];

    //Set delegate
    [xmlParser setDelegate:parser];

    //Start parsing the XML file.
    BOOL success = [xmlParser parse];

    if(success)
        NSLog(@"No Errors");
    else
        NSLog(@"Error Error Error!!!");

    // Configure and show the window
    [window addSubview:[navigationController view]];
    [window makeKeyAndVisible];
}


- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict;
{

    classelement=elementName;

    if([elementName isEqualToString:@"item"])
    {
        itemselected=YES;
        mutttitle=[[NSMutableString alloc] init];
        mutstrlink=[[NSMutableString alloc] init];
    }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName;
{
    if([elementName isEqualToString:@"item"])
    {
        itemselected=NO;

        [titarry addObject:mutttitle];
        [linkarray addObject:mutstrlink];

    }

}


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string;
{
    if (itemselected)
    {
        if ([classelement isEqualToString:@"title"])
        {
            [mutttitle appendString:string];
        }
        else if ([classelement isEqualToString:@"link"])
        {
            [mutstrlink appendString:string];
        }
    }
}



- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError;
{
    UIAlertView *alt=[[UIAlertView alloc] initWithTitle:@"RSS Reader"
                                                message:[NSString stringWithFormat:@"%@",parseError]
                                               delegate:nil
                                      cancelButtonTitle:@"Close"
                                      otherButtonTitles:nil];

    [alt show];


}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [titarry count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
    }

    cell.textLabel.text=[titarry objectAtIndex:indexPath.row];
    cell.accessoryType=UITableViewCellSelectionStyleBlue;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


    secondViewController *second = [[secondViewController alloc] initWithNibName:@"secondViewController" bundle:nil];
    [self.navigationController pushViewController:second animated:YES];
    NSURL *url=[NSURL URLWithString:[titarry objectAtIndex:indexPath.row]];
    NSURLRequest *req=[NSURLRequest requestWithURL:url];
    second.webView.scalesPageToFit=YES;
    [second.webView loadRequest:req];//here we have to perform changes try to do some things here


}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top