适用于iOS的Easy XML-Parser-目标C [英] Easy XML-Parser for iOS - Objective C
本文介绍了适用于iOS的Easy XML-Parser-目标C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找最易于实现"的方法来解析Objective-C移动应用程序中的XML.我尝试使用TBXML,但是我是一个新手,因此遇到了一些错误...您认为那里有些更简单的方法了吗?谢谢
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
推荐答案
这是非常简单的xml解析..
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
}
在.h文件中添加以下内容
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;
这篇关于适用于iOS的Easy XML-Parser-目标C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文