Parsing XML data in NSMutableArray iOS - iPhone

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 values โ€‹โ€‹of <categoryName> in my NSMutableArray categList .

The code I use 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 do not get category names in NSMutableArray categList :

What is wrong in my code ?: (

thanks

+4
source share
4 answers

Hey, you can use XMLReader , but it will return an NSDictionary .

The following code demonstration is for XMLReader to get NSDictionary .

import XMLReader in your file.

 #import "XMLReader.h" 

Then use the following method to get the NSDictionary . If you need to pass XML as NSString .

 NSDictionary* xmlDict = [XMLReader dictionaryForXMLString:theXML error:&er]; 
+7
source

If you want to use GDataXMLParser, you can use the following code that works great

The first Impost library file is GDataXMLParser, and 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
source

here I have a parsing of you r Application of its work is very good pls check that it makes the object orientation class of the category 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 work is good, try it.

thanks

+2
source

You use if ( [elementName isEqualToString:@"categList"])... but I cannot find the categList name in your XML example. Please check - this may be a problem.

+1
source

Source: https://habr.com/ru/post/1403595/


All Articles