加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

解析xml

发布时间:2020-12-16 06:01:57 所属栏目:百科 来源:网络整理
导读:方案一: #import " xmlViewController.h " @implementation xmlViewController static NSString * feedURLString = @" http://headlines.yahoo.co.jp/rss/sci.xml " ; - ( void )parserDidStartDocument:(NSXMLParser * )parser { // 解析开始时的处理 } -

方案一:

#import "xmlViewController.h"

@implementation xmlViewController


static NSString *feedURLString = @"http://headlines.yahoo.co.jp/rss/sci.xml";

- (void)parserDidStartDocument:(NSXMLParser *)parser
{
// 解析开始时的处理
}

- (void)parseXMLFileAtURL:(NSURL *)URL parseError:(NSError **)error
{
NSXMLParser
*parser = [[NSXMLParser alloc] initWithContentsOfURL:URL];
[parser setDelegate:self];
[parser setShouldProcessNamespaces:NO];
[parser setShouldReportNamespacePrefixes:NO];
[parser setShouldResolveExternalEntities:NO];
[parser parse];
NSError
*parseError = [parser parserError];
if (parseError && error) {
*error = parseError;
}
[parser release];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
// 元素开始句柄
if (qName) {
elementName
= qName;
}
if ([elementName isEqualToString:@"PicInfoListResult"]) {
// 输出属性值
//NSLog(@"Name is %@",[attributeDict objectForKey:@"Success"]);//,[attributeDict objectForKey:@"RecordCount"]);


}
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
// 元素终了句柄
if (qName) {
elementName
= qName;
}
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
// 取得元素的text
NSLog(@"%@",string);
}

- (void)viewDidLoad{
NSError
*parseError = nil;
[self parseXMLFileAtURL:[NSURL URLWithString:feedURLString] parseError:
&parseError];

[super viewDidLoad];
}

- (void)dealloc {
[super dealloc];
}

@end

  

方案二:

#import "xml2ViewController.h"

@implementation xml2ViewController

+(void) parseTest {
// 取得获取Yahoo新闻标题的RSS
NSString *urlStr = @"http://headlines.yahoo.co.jp/rss/sci.xml";
// 使用NSXMLParser三部曲:生成、设置代理和开始解析
NSXMLParser *parser = [[NSXMLParser alloc]
initWithContentsOfURL:[NSURL URLWithString:urlStr]];
xml2ViewController
*sp = [[xml2ViewController alloc]init];

[parser setDelegate:sp];
[parser parse];
NSError
*parseError = [parser parserError];
if (parseError ) {
NSLog(
@"Err %@",[parseError description]);
}
NSLog(
@"parseEnd");
}

- (void)viewDidLoad{
[xml2ViewController parseTest];

[super viewDidLoad];
}

-(id)init {
if (self = [super init]) {
tagPath
= [[[NSMutableArray alloc]init]retain];
tagPathAttributs
= [[[NSMutableArray alloc]init]retain];
[tagPath addObject:
@""];
[tagPathAttributs addObject:[NSDictionary dictionary]];
}
return self;
}
-(void)dealloc {
[tagPath release];
[tagPathAttributs release];
[super dealloc];
}

// 获取Tag的全路径
-(NSString*)tagFullPath {
return [tagPath componentsJoinedByString:@"/"];
}

// 开始解析
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString
*)elementName
namespaceURI:(NSString
*)namespaceURI
qualifiedName:(NSString
*)qName
attributes:(NSDictionary
*)attributeDict
{
NSString
*tag = [elementName copy];
[tagPath addObject:tag];
[tagPathAttributs addObject:[attributeDict copy]];

//开始获取文字列
NSString *tagFullPath = [self tagFullPath];
NSLog(
@"%@",tagFullPath);
if ([tagFullPath hasPrefix:@"/PicInfoListResult/Success"]) {
recordingText
= [[[NSMutableString alloc]init] autorelease];
[recordingText appendString:
@""];
NSLog(
@"%@",recordingText);
}
}

// Tag的完成
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString
*)namespaceURI qualifiedName:(NSString *)qName {
//结束获取文字列
NSString *tagFullPath = [self tagFullPath];
if ([tagFullPath hasPrefix:@"/PicInfoListResult/Success"]) {
NSLog(
@"Title:%@",recordingText);
recordingText
= nil;
}

[tagPath removeLastObject];
[tagPathAttributs removeLastObject];
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
// 记录所取得的文字列
if (recordingText!=nil) {
[recordingText appendString:
string];
}
NSLog(
@"%@",string);
}
- (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock{
//NSLog(@"cData:%@",[NSString stringWithUTF8String:[CDATABlock bytes]]);
}

@end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读