2 lpploverou LPPloveROU 于 2012.11.07 13:47 提问

使用NSXMLParser能不能解析只有一个node的xml?

我研究了NSXMLParser,然后准备实现一下,解析XML文件,代码如下:

-(OneNodeXMLParser*)initOneNodeXMLParser{

appDelegate = (OneNodeXMLParser*)[[UIApplication sharedApplication]delegate];
return self;
}

-(void)parser:(NSXMLParser*) parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
NSLog(@"%s",__PRETTY_FUNCTION__,nil);
if([elementName isEqualToString:@"a"]){

}
NSLog(@"Starting processing");
}

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
NSLog(@"%s",__PRETTY_FUNCTION__,nil);
NSLog(@"%s",string);
}

   -(void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
NSLog(@"%s",__PRETTY_FUNCTION__,nil);
NSLog(@"Finishing processing");
}

要成功解析应该改哪啊?

1个回答

love_loveec
love_loveec   2012.11.07 14:48
已采纳

在.h文件中声明一个NSMutableString *store;

然后加下面的代码:

-(void)parser:(NSXMLParser*) parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
  if([elementName isEqualToString:@"a"])
  {
     store = [[NSMutableString alloc] init];
  }
  NSLog(@"Starting processing");
}

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
  NSLog(@"%s",string);
   if (store != nil)
   {
     [store appendString:string];
   }
}

-(void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
 {
   NSLog(@"Finishing processing String : %@", store);
 }
LPPloveROU
LPPloveROU 谢谢
接近 5 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片