0

I am new to iOS development. I want to parse my xml web-service array. My xml response is like,

 <BookList>
     <Book>
           <BookID>int</BookID>
           <BookName>string</BookName>
           <Pages>string</Pages>
           <Price>string</Price>
     </Book>
     <Book>
           <BookID>int</BookID>
           <BookName>string</BookName>
           <Pages>string</Pages>
           <Price>string</Price>
     </Book>
 </BookList>

Here is the code i am using to parse the response,

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSString *theXML = [[NSString alloc] initWithBytes:
                        [self.webResponseData mutableBytes] length:[self.webResponseData length] encoding:NSUTF8StringEncoding];

    //now parsing the xml
    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData: self.webResponseData];
    xmlParser.delegate = self;

    // Run the parser
    @try{
        BOOL parsingResult = [xmlParser parse];
    }
    @catch (NSException* exception)
    {
        UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"Server Error" message:[exception reason] delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
        return;
    }
}

//Implement the NSXmlParserDelegate methods
-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:
(NSString *)qName attributes:(NSDictionary *)attributeDict
{
     currentElement = elementName;
}


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    // NSLog(@"String : %@", string);
    if ([currentElement isEqualToString:@"BookID"]) {
        NSLog(@"BookID : %@", string);
    }else if ([currentElement isEqualToString:@"BookName"]) {
        NSLog(@"BookName : %@", string);
    }else if ([currentElement isEqualToString:@"Pages"]) {
        NSLog(@"Pages : %@", string);
    }else if ([currentElement isEqualToString:@"Price"]) {
        NSLog(@"Price : %@", string);
    }

}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
   // NSLog(@"Current Element: %@ ",currentElement);
}

Using this code I can view the results, But i can get the number of books.

1 Answer 1

1

You can add this in to .h file

NSMutableDictionary *dictBook;
NSMutableArray *arrBook;
NSMutableString *elementValue;

Now use the below code to get the total book counts.

//Implement the NSXmlParserDelegate methods
-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:
(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    currentElement = elementName;
    if ([currentElement isEqualToString:@"BookList"]) {
        arrBook = [[NSMutableArray alloc] init];
    }
    else if ([currentElement isEqualToString:@"Book"]) {
        dictBook = [[NSMutableDictionary alloc] init];
    }
}


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if(!elementValue)
        elementValue = [[NSMutableString alloc] initWithString:string];
    else
      elementValue = string;  //[elementValue appendString:string];

    NSLog(@"elementValue : %@",elementValue);
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    // NSLog(@"Current Element: %@ ",currentElement);

    if ([elementName isEqualToString:@"BookID"] ||
        [elementName isEqualToString:@"BookName"] ||
        [elementName isEqualToString:@"Pages"] ||
        [elementName isEqualToString:@"Price"])
    {
        [dictBook setObject:elementValue forKey:elementName];
    }
    else if ([elementName isEqualToString:@"Book"])
    {
        [arrBook addObject:dictBook];
    }

   // [elementValue setString:@""];
}

- (void)parserDidEndDocument:(NSXMLParser *)parser {
     NSLog(@"Arr Book Count : %lu", (unsigned long)arrBook.count);

}

Hope this will help you.

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

3 Comments

@Bkjadev Here I am getting the count as correctly, But when i print the array like, - (void)parserDidEndDocument:(NSXMLParser *)parser { NSLog(@"Arr Book Count : %lu", (unsigned long)arrBook.count); NSLog(@"Book Array : %@", arrBook); } My log is like , Arr Book Count : 2 Book Array : ( { BookID = ""; BookName = ""; Pages = ""; Price = ""; }, { BookID = ""; BookName = ""; Pages = ""; Price = ""; } ) –
Remove one line == [elementValue setString:@""]; from the didEndElement function
I have changed "[elementValue appendString:string];" to elementValue = string; also aslo commented "[elementValue setString:@""];". Now its Working. Thanks.

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.