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