3

This is the XML data:

<Categorys> 
    <Category>  
        <categoryId>25</categoryId>  
            <categoryName>My Photos</categoryName>                                  
                <categoryDescription>Description</categoryDescription>      
            <categoryIconPath>7.jpg</categoryIconPath>      
            <userId>2</userId>  
            <categoryStatus>ON</categoryStatus>  
        <publicPrivate>Private</publicPrivate>
    </Category>
......more categories
<Categorys>

Here I want to get the <categoryName> values into my NSMutableArray categList.

The code I have used is:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI
                                       qualifiedName:(NSString *)qualifiedName 
                                          attributes:(NSDictionary*)attributeDict {

    if ( [elementName isEqualToString:@"categList"]) {
        // addresses is an NSMutableArray instance variable
        if (!categList)
            categList = [[NSMutableArray alloc] init];
        return;
    }

    NSLog(@"StartedElement %@", elementName);

    element = [NSMutableString string];

}


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {

    if(element == nil)

        element = [[NSMutableString alloc] init];

        [element appendString:string];

}

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

    NSLog(@"Found an element named: %@ with a value of: %@", elementName, element);

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

        NSLog(@"\n\n\nfound::::::::::::::: %@", element);

        category = element;

        NSLog(@"category:::: %@", category);

    }

    [categList addObject:element];
    NSLog(@"categList*************** %@", categList);
}

But I am not getting the category names in my NSMutableArray categList:

Whats wrong in my code? :(

Thanks

1
  • just use JSON, small overhead, less code, way faster parsing. Commented Mar 27, 2012 at 6:59

4 Answers 4

7

Hey You can use XMLReader but it will return NSDictionary.

The Following code demo for XMLReader to get NSDictionary.

import XMLReader in your file.

#import "XMLReader.h"

Then use following method to get NSDictionary. Where you have to pass your XML as NSString .

NSDictionary* xmlDict = [XMLReader dictionaryForXMLString:theXML error:&er];
Sign up to request clarification or add additional context in comments.

Comments

2

If you want to use GDataXMLParser you can use following code that is working fine

First Impost GDataXMLParser library file then write vbe

 -(void)XMLParsing
 {

      GdataParser *parser = [[GdataParser alloc] init];

      NSString* urlString = @"YOURURL"; 

      NSURL *url = [[NSURL alloc] initWithString:urlString];

      [parser downloadAndParse:url withRootTag:@"Category" withTags:[NSDictionary dictionaryWithObjectsAndKeys:@"categoryId",@"categoryId",@"categoryName",@"categoryName",@"categoryDescription",@"categoryDescription",@"categoryIconPath",@"categoryIconPath",@"userId",@"userId",@"categoryStatus",@"categoryStatus",@"publicPrivate",@"publicPrivate",nil] 
                         sel:@selector(finishGetDataSF:) 
                  andHandler:self];
      [urlSR release];
      [parser release];
  } 


  -(void)getDate:(NSDictionary*)dict
  {
      NSLog(@"Data from XML file  ==  %@",dict);

      NSArray* arrayTemp = [dict copy];
  }

2 Comments

I want to populate array with the required data so that I would use it in tableview
I think you can do NSArray* arrayTemp = [dict copy]; by this
2

here i have parsing of you r Application its working very fine pls check it make a category object orientation class and use it.

 -(void)loaddatafromxml
  {
    NSString *ur=[NSString stringWithFormat:@"Enter your url"]; 

     NSURL *url=[NSURL URLWithString:ur];
     NSMutableURLRequest *req=[NSMutableURLRequest requestWithURL:url];
     conn=[[NSURLConnection alloc]initWithRequest:req delegate:self];
     if(conn)
     webdata=[[NSMutableData data]retain];
  }

 -(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
 {
[webdata appendData:data];
  }
 -(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
  {
[webdata setLength:0];  
   }

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
categaryArry=[[NSMutableArray alloc]init];


  NSString *thexml=[[NSString alloc] initWithBytes:[webdata mutableBytes] length:[webdata length] encoding:NSASCIIStringEncoding];

  NSArray *arr=[thexml componentsSeparatedByString:@"<Category>"];


for(int i=1;i<[arr count];i++)
{
    categList *object = [[categList alloc] init];
    NSString *str=[arr objectAtIndex:i];


    NSArray *arr=[str componentsSeparatedByString:@"<categoryId>"];
    NSString *data=[arr objectAtIndex:1];
    NSRange ranfrom=[data rangeOfString:@"</categoryId>"];
    object.catid=[data substringToIndex:ranfrom.location];





    arr=[str componentsSeparatedByString:@"<categoryName>"];
    data=[arr objectAtIndex:1];
    ranfrom=[data rangeOfString:@"</categoryName>"];
   object.catname=[data substringToIndex:ranfrom.location];



    arr=[str componentsSeparatedByString:@"<categoryDescription>"];
    data=[arr objectAtIndex:1];
    ranfrom=[data rangeOfString:@"</categoryDescription>"];
   object.catdesc=[data substringToIndex:ranfrom.location];






    arr=[str componentsSeparatedByString:@"<categoryIconPath>"];
    data=[arr objectAtIndex:1];
    ranfrom=[data rangeOfString:@"</categoryIconPath>"];
    object.caticon=[data substringToIndex:ranfrom.location];





    arr=[str componentsSeparatedByString:@"<userId>"];
    data=[arr objectAtIndex:1];
    ranfrom=[data rangeOfString:@"</userId>"];
    object.userid=[data substringToIndex:ranfrom.location];





    arr=[str componentsSeparatedByString:@"<categoryStatus>"];
    data=[arr objectAtIndex:1];
    ranfrom=[data rangeOfString:@"</categoryStatus>"];
    object.catstatus=[data substringToIndex:ranfrom.location];





    arr=[str componentsSeparatedByString:@"<publicPrivate>"];
    data=[arr objectAtIndex:1];
    ranfrom=[data rangeOfString:@"</publicPrivate>"];
    object.publicpriv=[data substringToIndex:ranfrom.location];


    [categaryArry addObject:object];
}   

[self.table_data reloadData];

[thexml release];

[connection release];

 }

and call the loaddatafrm xml function as [self loaddatafromxml]; its working nice try it .

Thanks

Comments

1

You use if ( [elementName isEqualToString:@"categList"])... but I can't find categList name in you XML sample. Check please - this can be an issue.

Comments

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.