Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-26-2011, 11:31 PM   #1 (permalink)
XXX
Registered Member
 
Join Date: Dec 2009
Posts: 121
XXX is on a distinguished road
Default How to parse this XML structure ?

Hi

can any one please help on parsing this XML :

Code:
<?xml version="1.0" encoding="utf-8"?>
<XML>
<CATEGORY ID="1" name="cars">
<CATEGORYCHILD ID="2" name="TOYOTA"></CATEGORYCHILD>
<CATEGORYCHILD ID="3" name="BMW"></CATEGORYCHILD>
<CATEGORYCHILD ID="4" name="MITSUBISHI"></CATEGORYCHILD>
</CATEGORY>
<CATEGORY ID="5" name="Electronics">
<CATEGORYCHILD ID="6" name="TVs"></CATEGORYCHILD>
<CATEGORYCHILD ID="7" name="Calcs"></CATEGORYCHILD>
</CATEGORY>
</XML>
i need to store each Category and its sub category in a dictionary then the other category with its sub and finally all in one master nsmutablearray !

any help please !
XXX is offline   Reply With Quote
Old 08-26-2011, 11:42 PM   #2 (permalink)
XXX
Registered Member
 
Join Date: Dec 2009
Posts: 121
XXX is on a distinguished road
Default

this is the code i wrote but its not what i really want ..


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

	if ([elementName isEqualToString:@"CATEGORY"]) {
		
		item = [[[NSMutableDictionary alloc] init] autorelease];
		[item setObject:[attributeDict objectForKey:@"name"] forKey:@"MainCategName"];
			
	}
	 else if ([elementName isEqualToString:@"CATEGORYCHILD"]){
		[item setObject:[attributeDict objectForKey:@"ID" ]forKey:@"subCategID"];
		[item setObject:[attributeDict objectForKey:@"name" ]forKey:@"subCategName"];
		 
		 [masterArray addObject:[item copy]];
	 }
	
}

and the result is so wrong ...


Code:
(
        {
        MainCategName = cars;
        subCategID = 2;
        subCategName = TOYOTA;
    },
        {
        MainCategName = cars;
        subCategID = 3;
        subCategName = BMW;
    },
        {
        MainCategName = cars;
        subCategID = 4;
        subCategName = MITSUBISHI;
    },
        {
        MainCategName = Electronics;
        subCategID = 6;
        subCategName = TVs;
    },
        {
        MainCategName = Electronics;
        subCategID = 7;
        subCategName = Calcs;
    }
)


thanx
XXX is offline   Reply With Quote
Old 08-27-2011, 05:19 AM   #3 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Jan 2010
Location: Issaquah, WA
Age: 42
Posts: 1,244
dljeffery is on a distinguished road
Default

Can you explain what exactly is wrong? As in, what exactly are you trying to end up with?
__________________
Recall It! Tag your notes. Tag your photos. Tag your thoughts. Tag your life.

Recall It! for iPad

http://www.dljeffery.com
dljeffery is offline   Reply With Quote
Old 08-27-2011, 11:11 AM   #4 (permalink)
XXX
Registered Member
 
Join Date: Dec 2009
Posts: 121
XXX is on a distinguished road
Default

Hi dljeffery.thanx for replying.

here is how i want it

Code:
(
        {
        MainCategName = cars;
        subCategID = 2;
        subCategName = TOYOTA;
        subCategID = 3;
        subCategName = BMW;
        subCategID = 4;
        subCategName = MITSUBISHI;
    },
        {
        MainCategName = Electronics;
        subCategID = 6;
        subCategName = TVs;
        subCategID = 7;
        subCategName = Calcs;
    }
)
Regards
XXX is offline   Reply With Quote
Old 08-27-2011, 03:43 PM   #5 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Jan 2010
Location: Issaquah, WA
Age: 42
Posts: 1,244
dljeffery is on a distinguished road
Default

Can you explain what exactly you are trying to accomplish with this? As in, how would you interact with the structure? Do you look stuff up in it? If so, how do you do the lookup? Child ID? Child Name? Category Name? Some combination of these?
__________________
Recall It! Tag your notes. Tag your photos. Tag your thoughts. Tag your life.

Recall It! for iPad

http://www.dljeffery.com
dljeffery is offline   Reply With Quote
Old 08-27-2011, 05:03 PM   #6 (permalink)
XXX
Registered Member
 
Join Date: Dec 2009
Posts: 121
XXX is on a distinguished road
Default

i have a tableView with sections. for the title of the section it will be the name of the main category.in this case (cars). and the rows belong to this section will be filled with the names of the child categories.

and regarding how to retrieve it maybe something like this

Code:
cell.textLabel.text = [[masterArray objectAtIndex:indexPath.row] objectForKey:CHILD_NAME_KEY];
Regards !
XXX is offline   Reply With Quote
Old 08-27-2011, 05:57 PM   #7 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Jan 2010
Location: Issaquah, WA
Age: 42
Posts: 1,244
dljeffery is on a distinguished road
Default

OK. I was about to suggest a dictionary where category name maps to a sub dictionary which would contain your child id/names.

However, I'm glad I didn't. A dictionary is not an appropriate data source for a table view. Perhaps an array of category names, and then a dictionary mapping category name to a sub array of child names (do you actually need the child ID for anything?).

But even better would be a database. Unless your needs and the size of your data set are both very light.

How much data will you be handling, and where will you get it from? And how often will it change? Finally, what will you actually be doing with this table view? Displaying your data, sure, but for what ultimate purpose?
__________________
Recall It! Tag your notes. Tag your photos. Tag your thoughts. Tag your life.

Recall It! for iPad

http://www.dljeffery.com
dljeffery is offline   Reply With Quote
Old 08-27-2011, 09:44 PM   #8 (permalink)
XXX
Registered Member
 
Join Date: Dec 2009
Posts: 121
XXX is on a distinguished road
Default

Quote:
OK. I was about to suggest a dictionary where category name maps to a sub dictionary which would contain your child id/names.
this is exactly what i was looking for.
if you can please change the previous code or guide me on how to do exactly that.
and yes i really need the child ID with the name.so i can send it to the remote server which will save it in the user preferences and also to do other operations based on it.

thank you.
XXX is offline   Reply With Quote
Old 08-28-2011, 12:24 AM   #9 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Jan 2010
Location: Issaquah, WA
Age: 42
Posts: 1,244
dljeffery is on a distinguished road
Default

Quote:
Originally Posted by XXX View Post
this is exactly what i was looking for.
if you can please change the previous code or guide me on how to do exactly that.
and yes i really need the child ID with the name.so i can send it to the remote server which will save it in the user preferences and also to do other operations based on it.

thank you.
No, I can't... please re-read my previous post on why I don't recommend using a dictionary, after hearing what you are going to do with it.

You shouldn't use an unordered collection (such as a dictionary) as the data source for a table view.
__________________
Recall It! Tag your notes. Tag your photos. Tag your thoughts. Tag your life.

Recall It! for iPad

http://www.dljeffery.com
dljeffery is offline   Reply With Quote
Old 08-28-2011, 09:03 AM   #10 (permalink)
XXX
Registered Member
 
Join Date: Dec 2009
Posts: 121
XXX is on a distinguished road
Default

Quote:
Originally Posted by dljeffery View Post
No, I can't... please re-read my previous post on why I don't recommend using a dictionary, after hearing what you are going to do with it.

You shouldn't use an unordered collection (such as a dictionary) as the data source for a table view.
well, it seems i can't find the best way to do it. child names and ids are needed so i can't use your second suggestion. and using db is not practical also.

regards
XXX is offline   Reply With Quote
Old 08-28-2011, 01:45 PM   #11 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Jan 2010
Location: Issaquah, WA
Age: 42
Posts: 1,244
dljeffery is on a distinguished road
Default

Quote:
Originally Posted by XXX View Post
well, it seems i can't find the best way to do it. child names and ids are needed so i can't use your second suggestion. and using db is not practical also.

regards
You could easily create a class to hold your data, and then put instances of the class into an array.
__________________
Recall It! Tag your notes. Tag your photos. Tag your thoughts. Tag your life.

Recall It! for iPad

http://www.dljeffery.com
dljeffery is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 389
16 members and 373 guests
7twenty7, chiataytuday, cristofercolmbos, dedeys78, fiftysixty, gmarro, iOS.Lover, jonathandeknudt, kilobytedump, Matrix23, raymng, ryantcb, stanny, tymex, UMAD, xerohuang
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,669
Threads: 94,121
Posts: 402,903
Top Poster: BrianSlick (7,990)
Welcome to our newest member, dedeys78
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:15 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0