0

I have this fairly complex XML being returned from my server, I would like some help parsing it into a object I can acess for later use.

This is what the xml looks like

<Eng>
    <Result         >
        <Series         >
            <Link/>
            <FMF/>
                <AlsoLink/>
                <Plugins/>
            </FMF>
            <Sheet          >
                <Spaces>
                    <Spacing            >
                    <Names/>
                    </Spacing>
                    <Spacing            >
                    <Names/>
                    </Spacing>
                </Spaces>
            </Sheet>
        </Series>
    </Result>
</Eng>

I am then using NSXMLParser to try and parse all of the "ObjectForKey/s" of each element in the xml into their own dictionarys.. if that makes sense.

This is what my code is currently looking like

#pragma mark - Parsing lifecycle

- (void)startTheParsingProcess:(NSData *)parserData
{
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:parserData]; //parserData passed to NSXMLParser delegate which starts the parsing process
    [parser setDelegate:self];
    [parser parse]; // starts the event-driven parsing operation.
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if([elementName isEqualToString:@"Series"]){
        parsedMutableDictionary = [[NSMutableDictionary alloc] initWithDictionary:attributeDict];
        self.parsedDataArrayOfDictionaries = [NSMutableArray arrayWithCapacity:8];

        if ([elementName isEqualToString:@"Link"]) {

        }
        if ([elementName isEqualToString:@"FMF"]) {
            if ([elementName isEqualToString:@"AlsoLink"]) {

            }
            else if ([elementName isEqualToString:@"Plugins"]) {

            }
        }
        else if ([elementName isEqualToString:@"Sheet"]) {

            if ([elementName isEqualToString:@"Spaces"]) {

                if ([elementName isEqualToString:@"Spacing"]) {

                    if ([elementName isEqualToString:@"Names"]) {

                    }
                }
            }

        }
    }

}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{    
    if([elementName isEqualToString:@"Series"]){
         [parsedDataArrayOfDictionaries addObject:parsedMutableDictionary];

        if ([elementName isEqualToString:@"Link"]) {

        }
        if ([elementName isEqualToString:@"FMF"]) {
            if ([elementName isEqualToString:@"AlsoLink"]) {

            }
            else if ([elementName isEqualToString:@"Plugins"]) {

            }
        }
        else if ([elementName isEqualToString:@"Sheet"]) {

            if ([elementName isEqualToString:@"Spaces"]) {

                if ([elementName isEqualToString:@"Spacing"]) {

                    if ([elementName isEqualToString:@"Names"]) {

                    }
                }
            }

        }
    }


//     parsedMutableDictionary = nil;
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
    NSLog(@"Paser Error = %@", parseError);
    UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"A parsing failure occurred." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [errorAlert show];
}


//TODO: Look into how to use this method over multiple views. i.e.(other requests such as keycode, advanced )
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
    // display values
        NSArray *filteredArray = parsedDataArrayOfDictionaries;
        NSLog(@"%@", filteredArray);

}

The problem I am having is that I dont understand what I should have in their own objects... also when some of these values return I could have multiple Spacing elements and I just dont know how to handle that...

Any help or tips would be hugely appreciated, if I havent specified something just ask as my brain is just struggling to wrap itself around this problem.

thanks.

2
  • I'm not sure this is helpful, but, I'll just toss this out there and hopefully not get down voted - But, is there anyway you could change the output of the data from XML to JSON? Parsing JSON is a snap compred to XML. Commented Mar 20, 2013 at 20:59
  • unfortunatly not..I have to deal with what I am given thanks for the comment though. Commented Mar 20, 2013 at 21:22

2 Answers 2

2

Suppose your XML file....

<Series element1="something" element2="something">
    <Spaces>
         <Spacing>
              <Names>
                 Something
              </Names>
         </Spacing>
         <Spacing>
              <Names>Something</Names>
         </Spacing>
    </Spaces>
</Series>

To get the value of element1 you have to do.....

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{   
    if([currentElement isEqualToString:@"Series"]) {
        NSLog(@"%@",[attributeDict objectForKey:@"element1"]);
    }
}

To get the multiple values of Spacing you have to do....

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if([currentElement isEqualToString:@"name"]) {
       [currentSpacingName appendString:string];
       [currentSpacingName appendString:@"any character"]
    }
}

After that store the value into a dictionary with desire key in this method...

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

this is just an example i hope now you could solve your problem.

Sign up to request clarification or add additional context in comments.

1 Comment

BOOM! this is exactly what I was hoping to see... I kind of came to this conclusion but was having trouble getting it into my head in an undestandable manner let alone construct it into a question for someone else to help me with... Okay I will continue on with this and try to get it up and running.. Thank you for your help.
0

Your are using NSXMLParser.... so you will get the strings between the tag... <tag>strings</tag>

To get the multiple Spacing elements you can append all the strings in a NSString object..

1 Comment

yea, between my tags I have keys.. is that the correct work? elementone="1" elementtwo="2" etc. so I store them into a MutableDictionary then I put my mutableDictionary into an array... The thing I am confused with is the structure and multiple elements i.e. I have have multiple spacings... I cant get my head around how I should store them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.