-1

我正在使用 XML 解析器来获取我的数据。

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
{
currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
{
    if ( [elementName isEqualToString:@"country"] ) 
    {
        x++;
       [messages addObject:[NSDictionary dictionaryWithObjectsAndKeys:Id,@"ID",Name,@"NAME",Image,@"IMAGE",nil]];
    //**111**
         NSLog(@"Data in Message : %@", [messages objectAtIndex:(x-1)]);

        [Id setString:@""];
        [Name setString:@""];
        [Image setString:@""];
    }
    if ( [elementName isEqualToString:@"id"] )
    {
        [Id appendString:currentNodeContent];
    }
    if ( [elementName isEqualToString:@"name"] ) 
    {
        [Name appendString:currentNodeContent];
    }
    if ( [elementName isEqualToString:@"im"] ) 
    {
        [Image appendString:currentNodeContent];
    }
}

//在 // * 111 *打印消息,但是当我在调用 xml 解析器后执行此操作时,它不会打印:

chatParser = [[NSXMLParser alloc] initWithData:receivedData];
[chatParser setDelegate:self];
[chatParser parse];

 NSLog(@"\n\nMessages...\n\n%@", messages); //here there is no data printing

此时它打印为空..

消息。

(
        {
        ID = "";
        IMAGE = "";
        NAME = "";
    },
        {
        ID = "";
        IMAGE = "";
        NAME = "";
    },
        {
        ID = "";
        IMAGE = "";
        NAME = "";
    },
        {
        ID = "";
        IMAGE = "";
        NAME = "";
    },
        {
        ID = "";
        IMAGE = "";
        NAME = "";
    },

)

我已经定义了消息属性并对其进行了综合。在viewDidLoad我做了这个

 messages=[[NSMutableArray alloc] init];

谢谢你的帮助..

4

2 回答 2

0

使用下面的代码。其中表是 NSObject 类,其中 CaseId、PartyId、CartId 作为此类中的一个属性。如果您有 xml url,只需使用 URl 调用 loadXMLByURL 方法。解析后,您将获得 TableArray 中的每个对象,其中包含表具有上述属性的对象。

NSMutableString *currentNodeContent;
NSXMLParser     *parser;
Tweet           *currentTweet;
bool            isStatus;

-(id) loadXMLByURL:(NSString *)urlString
{
    _tweets         = [[NSMutableArray alloc] init];
    NSURL *url      = [NSURL URLWithString:urlString];
    NSData  *data   = [[NSData alloc] initWithContentsOfURL:url];
    parser          = [[NSXMLParser alloc] initWithData:data];
    parser.delegate = self;
    [parser parse];
    return self;
}

- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

    - (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
    {
        if ([elementname isEqualToString:@"Table"]) 
        {
            currentTable = [Table alloc];
                isStatus = YES;
        }
    }

- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if (isStatus) 
    {
        if ([elementname isEqualToString:@"CaseId"]) 
        {
            currentTable.CaseId = currentNodeContent;
        }
        if ([elementname isEqualToString:@"PartyId"]) 
        {
            currentTable.PartyId = currentNodeContent;
        }
        if ([elementname isEqualToString:@"CartId"]) 
        {
            currentTable.CartId = currentNodeContent;
        }

    }
    if ([elementname isEqualToString:@"Table"]) 
    {
        [self.tableArray addObject:currentTable];
        currentTable = nil;
        currentNodeContent = nil;
    }
}

如果您有任何疑问,请告诉我。

于 2012-09-06T07:29:44.830 回答
0

你的问题是你一直使用相同Id, Name, Image的对象和线条

[Id setString:@""];
[Name setString:@""];
[Image setString:@""];

在每个国家/地区元素的末尾将它们的值设置为“”。

它最好看起来像这样(未经测试,可能有一些错别字):

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
{
  currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
{
  if ([elementName isEqualToString:@"id"] )
  {
    [lastMessage setObject:currentNodeContent forKey:"ID"];
  }
  else if ( [elementName isEqualToString:@"name"] ) 
  {
    [lastMessage setObject:currentNodeContent forKey:"NAME"];
  }
  else if ( [elementName isEqualToString:@"im"] ) 
  {
    [lastMessage setObject:currentNodeContent forKey:"IMAGE"];
  }
  else if ( [elementName isEqualToString:@"country"] ) 
  {
    NSLog(@"Data in Message : %@", lastObject);
    [messages addObject:lastMessage];
    [lastMessage release];
  }
}

-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elName namespaceURI:(NSString *)uri 
{
   if ( [elementName isEqualToString:@"country"] )
   {
     lastMessage = [[NSMutableDictionary alloc]init];
   }
}
于 2012-09-06T07:48:21.453 回答