gpt4 book ai didi

ios - 适用于 iOS 的简单 XML 解析器 - Objective C

转载 作者:行者123 更新时间:2023-11-28 20:02:01 24 4
gpt4 key购买 nike

我正在寻找最“易于实现”的方法来在 Objective-C 移动应用程序中解析 XML。我尝试使用 TBXML,但我是新手,我遇到了几个错误……您认为那里有更简单的方法吗?谢谢

最佳答案

这是非常简单的xml解析..

- (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 文件中添加以下内容

@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 - 适用于 iOS 的简单 XML 解析器 - Objective C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23579687/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com