首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >解析嵌套的XML Objective C- NSXMLParser

解析嵌套的XML Objective C- NSXMLParser
EN

Stack Overflow用户
提问于 2011-07-16 03:21:47
回答 1查看 2.2K关注 0票数 0

全,

我有以下格式的XML:

代码语言:javascript
复制
<linked-list>
    <Description>
        <desc></desc>
        <IP></IP>
    </Description>
</linked-list>

语句的内部可以有无限多个。

我应该如何使用NSXMLParser来解析它?我当前的代码如下所示,但它不能正确解析。

代码语言:javascript
复制
@implementation XMLParser

@synthesize response;

- (XMLParser *) initXMLParser
{
    self = [super init];
    // init dictionary of response data 
    response = [[NSMutableDictionary alloc] init];
    return self;
}

//Gets Start Element of SessionData
- (void)parser:(NSXMLParser *)parser 
        didStartElement:(NSString *)elementName 
        namespaceURI:(NSString *)namespaceURI 
        qualifiedName:(NSString *)qualifiedName 
        attributes:(NSDictionary *)attributeDict 
{
    if ([elementName isEqualToString:@"linked-list"])
    {
        NSLog(@"Found linked-list in the return XML! Continuing...");
        //response is a NSMutableArray instance variable

        //THIS SHOULD NEVER NEED TO BE USED
        if (!response)//if array is empty, it makes it!
        {
            NSLog(@"Dictionary is empty for some reason, creating...");
            response = [[NSMutableDictionary alloc] init];
        }
        //END: THIS SHOULD NEVER BE USED
        return;
    }
    else
    {
        currentElementName = elementName;
        NSLog(@"Current Element Name = %@", currentElementName);
        return;
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
{
    if (!currentElementValue) {
        // init the ad hoc string with the value     
        currentElementValue = [[NSMutableString alloc] initWithString:string];
    } else {
        [currentElementValue setString:string];
        NSLog(@"Processing value for : %@", string);
    }
}  

//Gets End Element of linked-list
- (void)parser:(NSXMLParser *)parser 
 didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI 
 qualifiedName:(NSString *)qName {
    if ([elementName isEqualToString:@"linked-list"])
    {
        // We reached the end of the XML document
        // dumps dictionary into log
        NSLog(@"Dump:%@", [response description]);
        return;
    }
    else
    {
        //Adds key and object to dictionary
        [response setObject:currentElementValue forKey:currentElementName];
        NSLog(@"Set values, going around again... brb.");
    }
    currentElementValue = nil;
    currentElementName = nil;
}


@end
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-07-16 03:35:27

一些观察结果:

  1. WHAT内部的无限数量的WHAT?
  2. 假设可以有多个Description元素,则存储内容的外部数据结构必须是NSMutableArray而不是字典。然后,您在didStartElement:中为每个Description element.
  3. Consequently,使用一个可变字典,检查元素名称是否为@"Description",如果是,则创建一个新的NSMutableDictionary实例,并将其存储在一个ivar.
  4. 中。在foundCharacters:中,您总是必须将新字符附加到现有的currentElementValue中,因为该方法可以为每个元素的内容调用多次。我看到许多人这样做是错误的,尽管苹果的示例代码清楚地展示了正确的方法。
  5. In didEndElement:,这样做:

代码语言:javascript
复制
- If the element name is `@"desc"` or `@"IP"`, assign `currentElementValue` to the corresponding key in your current mutable dictionary. Don't forget to release `currentElementValue` before you set it to `nil`. You currently have a memory leak in your code because you're not doing that.
- If the element name is `@"Description"`, add the current mutable dictionary to the mutable array. Release the dictionary and set the ivar to `nil`. A new dictionary will be created the next time you encounter a `@"Description"` element in `didStartElement:`.
- If the element name is `@"linked-list"`, the mutable array will contain all the dictionaries and you're done.

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6712150

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档