iPhone利用xml传递数据,展示载Table界面中

效果图:

201008172010.jpg

 

iphone sdk已经给我们实现好了NSXMLParser,我们只需继承重写一些方法既可。

代码声明:

#import <Foundation/Foundation.h>

 

 

@interface XmlWelcome : NSXMLParser{

 

NSMutableArray *ones;

NSMutableDictionary *one;

int inOne;

int inTwo;

int Tag;

}

@property (nonatomic,retain) NSMutableArray *ones;

@end

 

声明实现:

 

#import “XmlWelcome.h”


@implementation XmlWelcome


@synthesize ones;


//开始一个dom树分析

- (void)parserDidStartDocument:(NSXMLParser *)parser {

NSLog(@”开始分析树结构);

ones = [NSMutableArray arrayWithCapacity:100];

 

}

//开始节点 内的信息

- (void)parser:(NSXMLParser *)parser

didStartElement:(NSString *)elementName

namespaceURI:(NSString *)namespaceURI

qualifiedName:(NSString *)qualifiedName

attributes:(NSDictionary *)attributeDict

{

 

NSLog(@”开始的节点是:%@”,elementName);

if ([elementName isEqual:@"title"]) {

Tag=1;

}else if ([elementName isEqual:@"content"]) {

Tag=2;

}

if ([elementName isEqual:@"item"]) {

NSString *lang=[attributeDict objectForKey:@"lang"];

if ([lang isEqual:@"zh"]) {

inOne=1;

}

}

if (inOne==1&&[elementName isEqual:@"value"]) {

inOne=0;

inTwo=1;

}

if ([elementName isEqual:@"image"]) {

NSString *message=[attributeDict objectForKey:@"src"];

[one setObject:message forKey:@"image"];

}

}

//结束节点

- (void)parser:(NSXMLParser *)parser

didEndElement:(NSString *)elementName

namespaceURI:(NSString *)namespaceURI

qualifiedName:(NSString *)qName

{

NSLog(@”结束节点:%@”,elementName);

if ([elementName isEqual:@"pavilion"]) {

Tag=0;

[ones addObject:one];

[one release];

}

}

// 结束dom

- (void)parserDidEndDocument:(NSXMLParser *)parser

{

 

NSLog(@”结束dom树分析);

}

 

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string

{

NSLog(@”节点的实际值:%@”,string);

if (inTwo==1) {

inTwo=0;

if (Tag==1) {

one=[[NSMutableDictionary alloc] initWithCapacity:10];

[one setObject:string forKey:@"title"];

}else if (Tag==2) {

[one setObject:string

forKey:@"content"];

}

}

}

@end

http://wangjun.easymorse.com/?p=829

posted @ 2010-10-20 18:47  周宏伟  阅读(503)  评论(0编辑  收藏  举报