需要帮助来解析xml字符串

时间:2022-06-20 02:04:20

i need to parse this xml file

我需要解析这个xml文件

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

i use this code to parse this.

我用这段代码来解析这个。

- (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];
}

But it adds only last object,displays like this in console.

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

test.......... (
    LION,
    "\n\n",
    "\n"
)

I need to add all image names in the xml file means {SQUIRREL,FOX,TIGER,LION}

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

how can i done,can any one please help me.

我该怎么办,请任何人帮助我。

Thank u in advance.

提前谢谢你。

3 个解决方案

#1


3  

Your problem is this method:

你的问题是这个方法:

- (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;
    }
}

Every time an image tag starts, you overwrite the test variable with a new instance of NSMutableArray (leaking the previous object). You need to change it to something like this:

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

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

That is: only allocate a new array if there wasn't an old one already.

即:只有在没有旧数组时才分配新数组。

#2


2  

- (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];
}

where test and mutableString are ivars declared in your view controller's header

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

Note: this is very simplified version.

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

#3


1  

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

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

#1


3  

Your problem is this method:

你的问题是这个方法:

- (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;
    }
}

Every time an image tag starts, you overwrite the test variable with a new instance of NSMutableArray (leaking the previous object). You need to change it to something like this:

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

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

That is: only allocate a new array if there wasn't an old one already.

即:只有在没有旧数组时才分配新数组。

#2


2  

- (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];
}

where test and mutableString are ivars declared in your view controller's header

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

Note: this is very simplified version.

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

#3


1  

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

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