2012-06-19 48 views
-1

任何人都可以幫助我解析這些數據。由於我是新手解析請任何人幫助我。無法在ios應用程序中解析xml

<?xml version="1.0" encoding="UTF-8"?> 
    <Tricon Type="Login"> 
      <Result>Invalid_Email_Password</Result> 
    </Tricon> 

我對解析器代碼是

#import "ResponseXmlParser.h" 

    @implementation ResponseXmlParser 
    @synthesize myParser; 

-(void)parserDidStartDocument:(NSXMLParser *)parser 
{ 
     printf("\n This is in parserDidStartDocument"); 
     response = @""; 
     type = @""; 
} 

ParseXMLFileWithData方法

-(void)parseXMLFileWithData:(NSData*)data 
{ 
     isRootNode = NO; 
     isResponse=NO; 

      myParser = [[NSXMLParser alloc] initWithData:data]; 
     [myParser setDelegate:self]; 
     [myParser setShouldProcessNamespaces:NO]; 
     [myParser setShouldReportNamespacePrefixes:NO]; 
     [myParser setShouldResolveExternalEntities:NO]; 
     [myParser parse]; 
     [myParser release]; 
    } 

didStartElement方法

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:  (NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict 
    { 
     printf("\n This is in didStartElement"); 
      if (qName) 
      { 
        elementName = qName; 
      } 
      if([elementName isEqualToString:@"Tricon"]) 
     { 
      printf("\n This is in didStartElement Tricon"); 
      if(responseObj != nil) 
      { 
      [responseObj release]; 
      } 
      responseObj = [[Response alloc] init] ; 
      isRootNode= YES; 
      isResponse=NO; 
      [email protected]""; 
      type = @""; 
      type=(NSString*)[attributeDict objectForKey:@"Type"]; 
    } 
     if([elementName isEqualToString:@"Result"]) 
     { 
     printf("\n This is in didStartElement Result"); 
     isResponse = YES; 
     } 
     } 

didEndElement方法

 -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
     { 
     printf("\n This is in didEndElement Result"); 
     if (qName) 
     { 
     elementName = qName; 
     } 
     if([elementName isEqualToString:@"Result"]) 
     { 
     printf("\n the element name is :%s",[elementName UTF8String]); 
     printf("\n the element name is :%s",[response UTF8String]); 

     [responseObj setResponse:response]; 
      printf("\n the element name is :%s",[responseObj.response UTF8String]); 
      isResponse=NO; 
     } 
     if([elementName isEqualToString:@"Tricon"]) 
     { 
      isRootNode = NO; 
      responseObj.type = type; 
      printf("\n The type is :%s\n",[responseObj.type UTF8String]); 
     } 
    } 

foundCharacters方法

 - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
     {  
     printf("\n This is in foundCharacters "); 
     if(isRootNode == YES) 
     { 
     if(isResponse == YES ) 
     { 
     string = [string stringByReplacingOccurrencesOfString:@"\n" withString:@" "]; 
      string = [string stringByReplacingOccurrencesOfString:@"\t" withString:@" "]; 
      string = [string stringByReplacingOccurrencesOfString:@"   " withString:@" "]; 
      string = [string stringByReplacingOccurrencesOfString:@"   " withString:@" "]; 
      string = [string stringByReplacingOccurrencesOfString:@"  " withString:@"\n\n\t"]; 
      string = [string stringByReplacingOccurrencesOfString:@"<p>" withString:@""]; 
      string = [string stringByReplacingOccurrencesOfString:@"</p>" withString:@"\n"]; 
      } 
      if(isResponse == YES) 
      { 
      response = [response stringByAppendingString:string]; 
      } 

      } 
      } 

parserDidEndDocument方法 - (無效)parserDidEndDocument:(的NSXMLParser *)解析器 {printf的 ( 「\ n這個是在parserDidEndDocument」);

  } 

我在這裏返回對象

 -(Response*)getMessageFromResponseParser 
     { 
     printf("\n the response value is :%s\n",[responseObj.response UTF8String]); 
     return responseObj; 
     } 

     -(void)dealloc 
     { 
    [responseObj release]; 
    [super dealloc]; 
     } 

    @end 
+0

你沒有告訴什麼是你有你的代碼的問題。 – san

+0

didStartElement方法未調用 – user1179590

+0

問題解決問題是在XML文件中 – user1179590

回答