0

我正在学校里解析 xml,我正在使用 Twitter 的 API 来处理。我正在尝试获取发布推文的日期,但遇到了一个问题:有两个同名元素在 xml.xml 中保存不同的值。但是,一个比另一个嵌套得更远,因此从逻辑上讲,我想检查嵌套的那个。我该怎么做呢?

现在一切都在完美运行。我只想为我的项目添加更多内容。

这是我正在调用的 didEndElement 方法的示例。

 -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:          (NSString *)namespaceURI qualifiedName:(NSString *)qName
{
//Creates an instance of the singleton to grab the array stored there
DataArraySingleton *singleton = [DataArraySingleton sharedArray];

//easy access to array
NSMutableArray *array = singleton.theArray;

//If the element ends with text
if ([elementName isEqualToString:@"text"])
{
    //Sets the value/key pair for text
    [tweets setValue:currentXMLValue forKey:elementName];

}
//Same as above
if ([elementName isEqualToString:@"screen_name"])
{
    [tweets setValue:currentXMLValue forKey:elementName];
}
//Same as above
if ([elementName isEqualToString:@"profile_image_url"])
{
    [tweets setValue:currentXMLValue forKey:elementName];
}
//If the element ends with status
if ([elementName isEqualToString:@"status"])
{
    //Adds the objects collected above to the array
    [array addObject:tweets];

    //Resets the object TweetInformation to be called again above
    tweets = nil;

}
//Resets xml value
currentXMLValue = nil;
}

多谢你们

4

1 回答 1

0

添加代码以检查所需嵌套日期元素的父标记。当您遇到开始标记时,请在代码中设置一个标志。现在,当您遇到日期标签时,请检查该标志是否已设置。如果未设置标志,则忽略日期标签。当您检测到父项的结束标记时,重置标志。

于 2013-02-25T02:59:53.417 回答