IOS中的XML解析在标签中显示NULL值?

时间:2011-12-27 15:23:47

标签: objective-c ios xml nsxmlparser nsmutablestring

我正在使用NSXMLParserDelegate进行XML解析。当我在标签中打印值时,我得到空值...我使用简单的xml文件得到了互联网。你也可以查看该文件中的内容。任何人请帮忙..这是我的代码

#import "xmlViewController.h"

@implementation xmlViewController
@synthesize label;

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"http://www.bonifacedesigns.com/tuts/xmltest.xml"]];
    receivedData=[[NSMutableData alloc]initWithContentsOfURL:url];
    dataParser=[[NSXMLParser alloc]initWithData:receivedData];
    dataParser.delegate=self;
    [dataParser parse];
}

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
//    if(!xmlString){
//        
//    }else
//    {
//        [xmlString setString:@""];
//    }
  if([elementName isEqualToString:@"elements"])
  {
      xmlString=[NSMutableString alloc];
      [xmlString setString:@""];
  }

}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    xmlString= (NSMutableString *)[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{

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

            NSString *string=xmlString;
            [xmlArray addObject:string];
            label.text=[NSString stringWithFormat:@"%@",xmlArray];

       }

}

@end

这是我的XML文件....我想打印XML Data Parsed!在我的标签中。帮助我。我已编辑代码并立即检查

<elements>
<element myData="XML Data Parsed!"/>
</elements>

检查一下:

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
//    if(!xmlString){
//        
//    }else
//    {
//        [xmlString setString:@""];
//    }


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

      xmlString=[[NSMutableString alloc]initWithCapacity:5];
  }

}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    xmlString= (NSMutableString *)[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{

        if([elementName isEqualToString:@"element"]){
            [attributeDict objectForKey:@"myData"];
            [xmlString setString:@""];
            NSString *string=xmlString;
            [xmlArray addObject:string];
            label.text=[NSString stringWithFormat:@"%@",xmlArray];

       }

我收到错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to mutate immutable object with setString:'
*** First throw call stack:
(0x13ba052 0x154bd0a 0x1362a78 0x13629e9 0x1387970 0x2a26 0x9e1a35 0x5171532 0x9e002d 0x26b8 0xd764e 0x37a73 0x37ce2 0x37ea8 0x3ed9a 0xfbe6 0x108a6 0x1f743 0x201f8 0x13aa9 0x12a4fa9 0x138e1c5 0x12f3022 0x12f190a 0x12f0db4 0x12f0ccb 0x102a7 0x11a9b 0x2238 0x2195 0x1)
terminate called throwing an exception

2 个答案:

答案 0 :(得分:1)

我想问题出在这一行:

xmlString== (NSMutableString *)[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

==更改为=

希望不需要解释。

答案 1 :(得分:1)

您的代码中似乎有多处错误。

可变字符串xmlString未在行上正确初始化:

xmlString = [NSMutableString alloc]; // wrong

这应该是:xmlString = [[NSMutableString alloc] initWithCapacity:X];

此外,当解析器找到字符时,您替换整个现有的可变字符串,而不是将它们附加到现有内容,或尝试,因为根据代码,您只需比较内存地址并浪费结果({ {1}})。

==

这应该是:

// wrong
xmlString == (NSMutableString *)[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

最后在[xmlString appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]]; 中,我不确定您要实现的目标,但是您将字符串对象引用添加到数组中,然后设置标签文本以显示数组的内容...只要可变数组被很好地分配和初始化(我真的不确定),标签应该至少显示数组内容。但是你可能会考虑“刷新”可变字符串(didEndElement)而不是在每个元素上分配它。

编辑

[xmlString setString:@""];将属性名称作为键及其值。我想您的属性值可以通过attributeDict访问。