从父对应方获取子元素的数据

时间:2013-08-26 11:10:03

标签: ios objective-c xml parsing nsxmlparser

我试图解析sub_category的子数据并显示它,但只会显示父类别的相关sub_category。我在解析父元素的数据方面非常成功,但我对如何解析子元素有疑问。

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
    if([elementName isEqualToString:@"category"]){
        dataCurrent = [dataFileHolder alloc];
    }
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {

    currentList  = [[NSMutableString alloc] initWithString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];

}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

    if([elementName isEqualToString:@"name"]){
        dataCurrent.nameOfCat = currentList;
    }
    if ([elementName isEqualToString:@"description"]){
        dataCurrent.descriptionOfCat = currentList;
    }
    if ([elementName isEqualToString:@"image"]) {
        dataCurrent.imageLink = currentList;
    }
    if ([elementName isEqualToString:@"category"]) {
        [listPopulated addObject:dataCurrent];
        dataCurrent = nil;
        currentList = nil;
    }
}

,XML文件就像这样

<category>
        <name>Food</name>
        <description>food description</description>
        <image> Link Here </image>
            <sub_cat>
                <sub_name>Sub name</sub_name>
                <sub_desc>sub cat description</sub_desc>
                <sub_image> Link </sub_image>
            </sub_cat>
            <sub_cat>
                <sub_name>Sub name</sub_name>
                <sub_desc>sub cat description</sub_desc>
                <sub_image> Link </sub_image>
            </sub_cat>
</category>

我一直在研究Event Driven XML Parsing,并且还从其中一个主题xml-parse-only-certain-child-elements中找到了gud引用,但最后我仍然对XML和解析内容感到困惑。我可能需要一个lamer术语。并且想知道如何进行解析部分。

2 个答案:

答案 0 :(得分:7)

我希望你可以使用ios native xml解析器本身。您的XML看起来非常简单。只是尝试为每个节点创建Dictionary并将其放入数组中。最后,您将获得一系列字典,您可以从中轻松迭代数据。

创建NSMutableArray *dataArray & NSMutableDictionary *dataDict & NSMutableDictionary *subcatDict NSMutableArray *subCatArray,在名为NSString的.h文件中也有一个currentElement

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
    currentElement = elementName;
    if([elementName isEqualToString:@"category"]){
        dataDict = [[NSMutableDictionary alloc] init];
    }
    else if ([elementName isEqualToString:@"sub_cat"]) {
        if(!subCatArray) {
             subCatArray = [[NSMutableArray alloc] init];
        }
        subcatDict = [[NSMutableDictionary alloc] init];
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    if(dataDict && !subCatArray){
        [dataDict setObject:string forKey:currentElement];
    }
    else if(subCatArray && subcatDict) {
        [subcatDict setObject:string forKey:currentElement];
    }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    if([elementName isEqualToString:@"category"]){
        [dataDict setObject:subCatArray forKey:@"sub_cat"];
        [dataArray addObject:dataDict];
        subCatArray = nil;
        dataDict = nil;
    }
    else if([elementName isEqualToString:@"sub_cat"]){
        [subCatArray addObject:subcatDict];
        subcatDict = nil;
    }
}

这会帮助你。

答案 1 :(得分:0)

尝试使用其中一个第三方XML解析器,例如RaptureXML

然后用以下内容解析数据:

RXMLElement *rootXML = [RXMLElement elementFromXMLData:yourXMLData];

并查询生成的根元素:

NSLog(@"Category name: %@", [rootXML child:@"name"].text);

[rootXML iterate:@"image.sub_cat" usingBlock: ^(RXMLElement *e) {
    NSLog(@"Sub name: %@", [e child:@"sub_name"].text);
}];