NSXMLParse 解析 soap消息

1. 启动NSXMLParser

要使用NSXMLParser要先创建它,设置各种属性,主要用到以下几个方法:

initWithContentsOfURL NSURL创建解析器

initWithData                NSData创建解析器, 例如:xmlPaser =[[NSXMLParse alloc] initWithData: webData];

setDelegate                 为解析器定义委托

parse                            运行解析器

2. 充当委托

最重要的5个方法:

//发现元素开始符的处理函数 (即报告元素的开始以及元素的属性)

方法1.  - (void)parser:(NSXMLParser *)parser

        didStartElement:(NSString*)elementName

        namespaceURI:(NSString*)namespaceURI

        qualifiedName:(NSString *)qName

        attributes:(NSDictionary*)attributeDict


方法2. //处理标签包含内容字符 报告元素的所有或部分内容)

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


方法3.//发现元素结束符的处理函数,保存元素各项目数据(即报告元素的结束标记)

- (void)parser:(NSXMLParser *)parser

        didEndElement:(NSString*)elementName

        namespaceURI:(NSString*)namespaceURI

        qualifiedName:(NSString *)qName


方法4.//报告解析的结束

- (void)parserDidEndDocument:(NSXMLParser*)parser


方法 5.//报告不可恢复的解析错误

- (void)paser:parserErrorOccured

 

  • 下面详细举例

1.申明了一个xml处理协议,用到NSXMLParser协议,用来具体处理我们要解析的xml文件,把需要的url取出来,存到指定的data里。

*/

//线程处理类的定义,方法实现,与向主线程回传data

@classAppRecord;


//@protocol ParseOperationDelegate;

@protocol ParseOperationDelegate

- (void)didFinishParsing:(NSArray *)appList;

- (void)parseErrorOccurred:(NSError *)error;

@end


@interface ParseOperation : NSOperation<NSXMLParserDelegate> {

   @private id <ParseOperationDelegate> delegate;//,NSXMLParserDelegate

 

    NSData *dataToParse;

 

NSMutableArray *workingArray;

AppRecord *workingEntry;

NSMutableString *workingPropertyString;

NSArray*elementsToParse;

BOOLstoringCharacterData;

}


- (id)initWithData:(NSData *)datadelegate:(id <ParseOperationDelegate>)theDelegate;


@end

然后完成协议的初始化函数

- (id)initWithData:(NSData *)datadelegate:(id <ParseOperationDelegate>)theDelegate

{

self =[super init];

if(self != nil)

{

self.dataToParse =data;

self.delegate =theDelegate;

self.elementsToParse= [NSArrayarrayWithObjects:kIDStr, kNameStr, kImageStr,kArtistStr, nil];

}

return self;

}


2.完成特定标签的解析

#pragma mark -

#pragma mark RSS processing


- (void)parser:(NSXMLParser*)parser didStartElement:(NSString*)elementName

namespaceURI:(NSString *)namespaceURI

qualifiedName:(NSString *)qName

attributes:(NSDictionary *)attributeDict

{

// entry: { id (link), im:name (app name), im:image(variable height) }

//

if([elementName isEqualToString:kEntryStr])

{

self.workingEntry =[[[AppRecord alloc] init] autorelease];

}

storingCharacterData = [elementsToParse containsObject:elementName];

}


3.找到具体的地址与内容,并存

- (void)parser:(NSXMLParser*)parser didEndElement:(NSString*)elementName

namespaceURI:(NSString *)namespaceURI

qualifiedName:(NSString *)qName

{

if(self.workingEntry)

{

if(storingCharacterData)

{

NSString*trimmedString = [workingPropertyStringstringByTrimmingCharactersInSet:

[NSCharacterSet whitespaceAndNewlineCharacterSet]];

[workingPropertyStringsetString:@""]; // clear the string for next time

if([elementName isEqualToString:kIDStr])

{

self.workingEntry.appURLString = trimmedString;

}

elseif ([elementName isEqualToString:kNameStr])

{

self.workingEntry.appName = trimmedString;

}

elseif ([elementName isEqualToString:kImageStr])

{

self.workingEntry.imageURLString = trimmedString;

}

elseif ([elementName isEqualToString:kArtistStr])

{

self.workingEntry.artist = trimmedString;

}

}

elseif ([elementName isEqualToString:kEntryStr])

{

[self.workingArray addObject:self.workingEntry];

self.workingEntry =nil;

}

}

 

}


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

{

if(storingCharacterData)

{

[workingPropertyString appendString:string];

}

}

你可能感兴趣的:(NSXMLParse 解析 soap消息)