-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これは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

パーサーデリゲートで多くの余分な作業を行っているようです。次のようなクリーンで簡潔な例から始めることをお勧めします。

シンプルで簡潔なデスクトップCocoaNSXMLParserの例?

必要なものを変更します

于 2012-06-19T13:59:17.713 に答える