سؤال

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.

هل كانت مفيدة؟

المحلول

I hope you can use ios native xml parser itself. Your XML looks pretty simple to handle. Just try to create Dictionary for each node and put it in array. So finally you will get array of dictionaries from which you can easily iterate your data.

Create NSMutableArray *dataArray & NSMutableDictionary *dataDict & NSMutableDictionary *subcatDict NSMutableArray *subCatArray, Also have one NSString in .h file named 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;
    }
}

This will help you out.

نصائح أخرى

Try one of the third-party XML parsers, for example RaptureXML.

Then you parse the data with:

RXMLElement *rootXML = [RXMLElement elementFromXMLData:yourXMLData];

and query the resulting root Element:

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

[rootXML iterate:@"image.sub_cat" usingBlock: ^(RXMLElement *e) {
    NSLog(@"Sub name: %@", [e child:@"sub_name"].text);
}];    
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top