6

im trying to parse the child data which is sub_category and show it, but will only show the relevant sub_category of the parent category. I was sucessful in parsing the data of the parent element but im having a problem on how to parse the child element.

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

and the XML file is like this

<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>

and I been researching about the Event Driven XML Parsing and also find a gud reference from one of the threads xml-parse-only-certain-child-elements, but at the end im still pretty confuse about the XML and parsing stuff. I might need a lamer term. And would like to know on how to do my parsing part.

4

2 回答 2

7

我希望您可以使用 ios 本机 xml 解析器本身。您的 XML 看起来很容易处理。只需尝试为每个节点创建 Dictionary 并将其放入数组中。所以最后你会得到一个字典数组,你可以很容易地从中迭代你的数据。

创建NSMutableArray *dataArray & NSMutableDictionary *dataDict & NSMutableDictionary *subcatDict NSMutableArray *subCatArray,在 .h 文件中也有一个NSString名为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;
    }
}

这将帮助你。

于 2013-08-29T15:40:39.570 回答
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);
}];    
于 2013-08-29T14:41:16.507 回答