English 中文(简体)
需要帮助解析xml字符串
原标题:need help to parse xml string
  • 时间:2011-02-10 06:18:59
  •  标签:
  • iphone

我需要解析这个xml文件

<?xml version="1.0" encoding="UTF-8"?>
<imageset>
<category>
<image>SQUIRREL</image>
<image>FOX</image>  
<image>TIGER</image>    
<image>LION</image> 
</category>
</imageset>

我用这段代码来解析它。

- (void)viewDidLoad {
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:[NSData dataWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Config.xml"]]];
    [super viewDidLoad];
    [parser setDelegate:self];
    [parser parse];
    NSLog(@"test.......... %@",test);
    }
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{            
    if ([elementName isEqualToString:@"image"]) {
        test = [[NSMutableArray alloc]init];
        addelements = TRUE;
    }
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{     
    if ([elementName isEqualToString:@"image"]) {
        addelements = FALSE;
    }
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
        [test addObject:string];
}

但它只添加了最后一个对象,在控制台中显示为这样。

test.......... (
    LION,
    "

",
    "
"
)

我需要在xml文件中添加所有图像名称,意思是{SQUIRREL,FOX,TIGER,LION}

我该怎么办,有人能帮我吗。

提前谢谢你。

最佳回答

您的问题在于这种方法:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{            
    if ([elementName isEqualToString:@"image"]) {
        test = [[NSMutableArray alloc]init];
        addelements = TRUE;
    }
}

每次启动图像标记时,都会使用NSMutableArray的新实例覆盖测试变量(泄漏上一个对象)。您需要将其更改为以下内容:

        if (!test) test = [[NSMutableArray alloc]init];

也就是说:只有在没有旧数组的情况下才分配新数组。

问题回答
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{ 
    [mutableString release];
    mutableString  = [[NSMutableString alloc] init];           
    if ([elementName isEqualToString:@"image"]) {
        if( !test  )  test = [[NSMutableArray alloc] initWithCapacity: 10];
    }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{   
    if ([elementName isEqualToString:@"image"])  
        [test addObject: mutableString];
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
       [mutableString appendString: string];
}

其中test和mutableString是在视图控制器的头中声明的ivar

注意:这是非常简化的版本。

@Mahesh Babu yes you can also add the image  by giving the name only as for example......    

[UIImage imageNamed:@"SQUIRREL.jpg"];




相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签