许多NSXMLParser在单个视图中使用xcode

时间:2012-10-12 06:55:44

标签: xcode4 nsxmlparser

我必须在单个视图中解析两个XML文件,所以我使用两个NSXMLParsers来获取它。但是问题是一个解析器被识别并且第二个解析器没有得到识别...我的意思是在调试器通过之后这行[parser1 parse]它应该转到委托didStartElement,foundcharacters和didEndElement,但在我的情况下调试器没有进入那些委托。它只是跳转到[parser1 release] stmnt.So那些委托没有得到parser1的识别。是这样吗? 我写了这个:

 NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:Url];      
 [parser setDelegate:self];
 [parser setShouldProcessNamespaces:NO];
 [parser setShouldReportNamespacePrefixes:NO];
 [parser setShouldResolveExternalEntities:NO];
 [parser parse];
 [parser release];

  NSXMLParser *parser1 = [[NSXMLParser alloc] initWithContentsOfURL:Url1];      
[parser1 setDelegate:self];
[parser1 setShouldProcessNamespaces:NO];
[parser1 setShouldReportNamespacePrefixes:NO];
[parser1 setShouldResolveExternalEntities:NO];
[parser1 parse];
[parser1 release];


 - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict 
{
  if([elementName isEqualToString:@"TimeList"])
 {
 arr1=[[NSMutableArray alloc] init];
 drr1=[[NSMutableDictionary alloc] init];

 }
 if([elementName isEqualToString:@"Company"])
 { 
 drr1=[[NSMutableDictionary alloc] init];        
 }
 srr1=[[NSMutableString alloc] init];

 } 


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


 [srr1 appendString:string];
 }


 - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

 if([elementName isEqualToString:@"ID"])
 {

 [drr1 setObject:srr1 forKey:@"ID"];
 [srr1 release],srr1=nil;
 return;
 }  

 if([elementName isEqualToString:@"Company"]) 
 { 
 [arr1 addObject:drr1]; 
 }    


 if([elementName isEqualToString:@"TimeList"])
 {

[drr1 release];

 }

  [srr1 release],srr1=nil; 
 }




- (void)parser1:(NSXMLParser *)parser1 didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict 
{
    if([elementName isEqualToString:@"categorylist"])
    {
        arr2=[[NSMutableArray alloc] init];
        drr2=[[NSMutableDictionary alloc] init];

    }
    if([elementName isEqualToString:@"lookup"])
    { 
        drr2=[[NSMutableDictionary alloc] init];        
    }
    srr2=[[NSMutableString alloc] init];

} 


-(void)parser1:(NSXMLParser *)parser1 foundCharacters:(NSString *)string{


    [srr2 appendString:string];
}


- (void)parser1:(NSXMLParser *)parser1 didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

    if([elementName isEqualToString:@"Code"])
    {

        [drr2 setObject:srr2 forKey:@"Code"];
        [srr2 release],srr2=nil;
        return;
    }  

    if([elementName isEqualToString:@"lookup"]) 
    { 
        [arr2 addObject:drr2]; 
    }    


    if([elementName isEqualToString:@"categorylist"])
    {

        [drr2 release];

    }

    [srr2 release],srr2=nil; 
    }

第一个解析器被识别并获取数据。但是对于第二个解析器,didStartElement没有得到识别......可能的原因是什么?

1 个答案:

答案 0 :(得分:2)

我自己解决了。实际上我声明了一个字符串并向它传递了一些值。解析它时检查,如果该条件为真,那么它会解析.. 这就是我所做的:

NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:Url];      
[parser setDelegate:self];
[parser setShouldProcessNamespaces:NO];
[parser setShouldReportNamespacePrefixes:NO];
[parser setShouldResolveExternalEntities:NO];
currentparser=@"1";
[parser parse];
[parser release];

  NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:Url1];      
[parser setDelegate:self];
[parser setShouldProcessNamespaces:NO];
[parser setShouldReportNamespacePrefixes:NO];
[parser setShouldResolveExternalEntities:NO];
currentparser=@"2";
[parser parse];
[parser release];

 - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict 
 {
 if(currentparser==@"1")
 {
     //do something
 } 
 if(currentparser=@"2")
 {
     //do something
  }
  }

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


   if(currentparser==@"1")
     {
        //do something
     }
   if(currentparser=@"2")
    {
       //do something
    }
    }


    - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

   if(currentparser==@"1")
    { 
     //do something
    }
    if(currentparser=@"2")
    {
      //do something
     }
     }