Getting data of Child Element from its Parent counterpart
Asked Answered
P

2

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.

Pleurisy answered 26/8, 2013 at 11:10 Comment(0)
S
7

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.

Solecism answered 29/8, 2013 at 15:40 Comment(5)
i've done this until this part, im just missing now is passing the child data which are inside the <sub_cat></sub_cat> tag from my parent view controller to my child view controller.Pleurisy
then create array of subcat and save that array in dataDict using subcat key.Solecism
see the updated answer. Now you will have one dataDict in dataArray. from which you can get your sub_cat array using sub_cat key. Then you can pass it to your view controller.Solecism
would you help me one last tym, I want to pass the value that is in my sub_cat which is the sub_name, sub_desc and sub_image to my childView controller, may i know how is it possible, coz i've done my code until this far too. and thanx a bunch.Pleurisy
Hey you got the subCat array right?? that will have 2 dict as of in your sample XML. Each will have key value pairs. So for first sub_cat. [subCatArray objectAtIndex:0] you will get subCatDict. then from subCatDict, [subCatDict valueForKey:(sub_desc or sub_image or sub_name)] will give you all the values. Thats it comes in handy.Solecism
S
0

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);
}];    
Selfaggrandizement answered 29/8, 2013 at 14:41 Comment(2)
can you please explain a little on to how i parse the given data, I've done the parsing now but i would like to know some other ways too like using the raptureXML, and btw im implementing this on storyboard where i show the parent data on the first tablevView then the child in the next view.Pleurisy
You can parse your data by constructing an RXMLElement. The query the resulting object for single elements by using the child: method and for mutiple elements by using iterate:usingBlock: like above.Selfaggrandizement

© 2022 - 2024 — McMap. All rights reserved.