-1

任何人都可以帮助我如何解析这些数据。由于我是解析新手,请任何人帮助我。

  <?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;
          response=@"";
          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 方法 - (void)parserDidEndDocument:(NSXMLParser *)parser { printf("\n This is in parserDidEndDocument ");

          }

我在这里返回对象

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

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

   @end
4

1 回答 1

0

看起来您在解析器委托中做了很多额外的工作。您可能希望从一个干净、简洁的示例开始,如下所示:

简单简洁的桌面 Cocoa NSXMLParser 示例?

并改变你所需要的

于 2012-06-19T13:59:17.713 回答