Easy XML-Parser for iOS - Objective C

天大地大妈咪最大 提交于 2019-12-12 02:47:27

问题


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


回答1:


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;



回答2:


- (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


}


来源:https://stackoverflow.com/questions/23579687/easy-xml-parser-for-ios-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!