解析嵌套的 XML 目标 C - NSXMLParser

Bau*_*aub 0 iphone xcode objective-c nsxmlparser

全部,

我有以下格式的 XML:

<linked-list>
    <Description>
        <desc></desc>
        <IP></IP>
    </Description>
</linked-list>
Run Code Online (Sandbox Code Playgroud)

这个 XML 语句在 <linked-list></linked-list> 中可以有无数个 <Description></Description>。

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

@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
Run Code Online (Sandbox Code Playgroud)

Ole*_*ann 5

一些观察:

  1. WHAT里面有无数的WHAT?

  2. 假设可以有多个Description元素,则存储内容的外部数据结构必须是NSMutableArray,而不是字典。然后,您可以为每个Description元素使用一个可变字典。

  3. 因此,在 中didStartElement:,检查元素名称是否是@"Description",如果是,则创建一个NSMutableDictionary存储在 ivar 中的新实例。

  4. 在 中foundCharacters:,您始终必须将新字符附加到现有字符中,currentElementValue因为可以为每个元素的内容多次调用该方法。尽管 Apple 的示例代码清楚地展示了正确的方法,但我看到很多人都做错了。

  5. 在 中didEndElement:,执行以下操作:

    • 如果元素名称是@"desc"@"IP",则分配currentElementValue给当前可变字典中的相应键。currentElementValue在设置为 之前不要忘记释放nil。您当前的代码中存在内存泄漏,因为您没有这样做。

    • 如果元素名称为@"Description",则将当前可变字典添加到可变数组中。释放字典并将 ivar 设置为nil. 下次遇到 中的@"Description"元素时,将创建一个新字典didStartElement:

    • 如果元素名称是@"linked-list",可变数组将包含所有字典,您就完成了。