// parser xml
解析xml需要NSXMLParserDelegate 协议,然后再实现类中完成重写parser等方法
NSAutoreleasePool 这个一定要有,不然会报错
一定要记得,在分析XML的时候,xml格式是如下的话,是从item开始读取,而不是aaa表节点,不然只读取依次循环就没有了。
<item>
<aaa>
<tt> fsdfs</tt>
<bb>fsdlkfjkls</bb>
</aaa>
<aaa>
<tt> fdsfds</tt>
<bb>fwefwwe</bb>
</aaa>
</item>
//interface
#import <Foundation/Foundation.h>
@interface IVPaserXMLSingleton : NSObject<NSXMLParserDelegate > {
NSMutableString *currentString ;
NSString *currentElement ;
}
@property (nonatomic ,retain )NSMutableString *currentString;
@property (nonatomic ,retain )NSString *currentElement;
-(void )getData:(NSString *)urlStr;
@end
//implements class
#import "IVPaserXMLSingleton.h"
#import "UserInfo.h"
@implementation IVPaserXMLSingleton
@synthesize currentString,currentElement;
-(void )getData:(NSString *)urlStr
{
[NSThread detachNewThreadSelector :@selector (parserData :) toTarget :self withObject :[NSURL URLWithString :urlStr]];
}
-(void )parserData:(NSURL *)url
{
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc ] init ];
NSXMLParser *parser=[[NSXMLParser alloc ] initWithContentsOfURL :url];
[parser setDelegate :self ];
[parser setShouldProcessNamespaces :YES ];
[parser setShouldReportNamespacePrefixes :YES ];
[parser setShouldResolveExternalEntities :NO ];
[parser parse ];
[pool drain ];
}
//start element
- (void )parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict
{
[currentElement release ];
currentElement =[elementName copy ];
//NSLog(qName);
//NSLog(@"I Jus found a start tag for %@",elementName);
if ([currentElement isEqualToString :@"item" ])
{
//NSLog(@"begin");
//[self performSelectorOnMainThread:@selector(create) withObject:nil waitUntilDone:YES];
}
else if ([currentElement isEqualToString :@"UserInfo" ])
{
self .currentString =[NSMutableString string ];
}
}
//found character
-(void )parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if ([currentElement isEqualToString :@"Id" ]){
// [currentString appendString:string];
// [currentString appendString:@" "];
}
else if ([currentElement isEqualToString :@"UserId" ]) {
// [currentString appendString:string];
// [currentString appendString:@" "];
}
else if ([currentElement isEqualToString :@"Name" ]) {
[currentString appendString :string];
// [currentString appendString:@" "];
}
else if ([currentElement isEqualToString :@"Sex" ])
{
// [currentString appendString:string];
// [currentString appendString:@" "];
}
else if ([currentElement isEqualToString :@"Age" ])
{
//[currentString appendString:string];
//[currentString appendString:@" "];
}
}
//parser over
-(void )parserDidEndDocument:(NSXMLParser *)parser
{
NSLog (@"Document finished!!!" );
}
//end element
-(void )parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if([elementName isEqualToStr ing:@"Id" ])
{
// NSLog(curr entString);
}
else if ([ currentElement isEqualToString :@"UserId"]) {
// NSLog(current String);
}
else if ([currentElement isEqualToStri ng:@"Name "]) {
// NSLog(cur rentString);
}
else if ([currentElement isEqualToStri ng:@"Sex" ])
{
// NSLog(cur rentString);
}
else if ([currentElem ent isEqualTo String:@"Age"])
{
NSL og(currentString) ;
}
}
@end