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 04-21-2009, 08:50 AM   #1 (permalink)
New Member
 
Join Date: Apr 2009
Posts: 16
ashwanik04 is on a distinguished road
Red face How to convert XML to dictionary

I need help in parsing XML
<Grandfather>
<Son name="1">
<Grandson name="1" />
</Son>
<Son name="2">
<Grandson name="1" />
</Son>
<Son name="3">
<Grandson name="1" />
<Grandson name="2" />
<Grandson name="3" />
</Son>
</Grandfather>

Can any body tell me how to convert this XML into dictionary
of the format
key(son) value(NSArray of grandsons)
Thanks in advance
ashwanik04 is offline   Reply With Quote
Old 04-21-2009, 09:01 AM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

It looks like you're gonna have to parse the XML and build the dictionary manually.

Look into NSXMLParser, it's quite easy to use.
smithdale87 is offline   Reply With Quote
Old 04-21-2009, 09:06 AM   #3 (permalink)
New Member
 
Join Date: Apr 2009
Posts: 16
ashwanik04 is on a distinguished road
Default XML to Dictionary

Quote:
Originally Posted by smithdale87 View Post
It looks like you're gonna have to parse the XML and build the dictionary manually.

Look into NSXMLParser, it's quite easy to use.
I tried but with no luck
More help is required as I am new to iPhone application development
ashwanik04 is offline   Reply With Quote
Old 04-21-2009, 09:12 AM   #4 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

can you post your code, and then we can discuss what you are doing wrong, or possibly a different approach?
smithdale87 is offline   Reply With Quote
Old 04-21-2009, 03:36 PM   #5 (permalink)
New Member
 
Join Date: Apr 2009
Posts: 2
epicjorge is on a distinguished road
Default

If you can get your xml data in plist format it would be much easier to do.
epicjorge is offline   Reply With Quote
Old 04-21-2009, 06:27 PM   #6 (permalink)
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: ¿La Islas Hermosas?
Posts: 2,176
Kalimba is on a distinguished road
Default

Quote:
Originally Posted by ashwanik04 View Post
I tried but with no luck
More help is required as I am new to iPhone application development
There's really no iPhone SDK API that I'm aware of that gives you this functionality for "free", so it may actually require some real "programming" to achieve this.
Kalimba is offline   Reply With Quote
Old 08-05-2009, 07:14 AM   #7 (permalink)
Registered Member
 
Join Date: Jul 2009
Location: calicut
Posts: 7
vineeth_c is on a distinguished road
Thumbs down convert xml to nsdictionary

use tinyxml to parse dictionary. this is a light weight c++ library..
vineeth_c is offline   Reply With Quote
Old 08-05-2009, 02:51 PM   #8 (permalink)
Registered Member
 
kelvinkao's Avatar
 
Join Date: Jul 2009
Location: Los Angeles
Posts: 352
kelvinkao is on a distinguished road
Send a message via AIM to kelvinkao
Default

I think you will find this helpful.
Event-Driven XML Programming Guide for Cocoa: Introduction to Event-Driven XML Programming Guide for Cocoa
kelvinkao is offline   Reply With Quote
Old 12-15-2010, 02:52 AM   #9 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 5
Benoit Caccinolo is on a distinguished road
Default

To easily convert XML to NSDictionary, here is a little lib to do this conversion:

XML to NSDictionary Converter bcaccinolo's blog

The code is on Github.

Cheers,
Benoit Caccinolo is offline   Reply With Quote
Old 06-16-2011, 02:54 AM   #10 (permalink)
Registered Member
 
Join Date: Jun 2011
Posts: 5
Cœur is on a distinguished road
Default

improved code of bcaccinolo's code:
https://github.com/Insert-Witty-Name...o-NSDictionary

You simply do:
Code:
NSDictionary *dic = [XMLReader dictionaryForXMLString:XML error:nil];
You get:
Code:
{
    Grandfather =     {
        Son =         (
                        {
                Grandson =                 {
                    name = 1;
                };
                name = 1;
            },
                        {
                Grandson =                 {
                    name = 1;
                };
                name = 2;
            },
                        {
                Grandson =                 (
                                        {
                        name = 1;
                    },
                                        {
                        name = 2;
                    },
                                        {
                        name = 3;
                    }
                );
                name = 3;
            }
        );
    };
}
Cœur is offline   Reply With Quote
Old 11-08-2011, 12:46 PM   #11 (permalink)
Registered Member
 
Join Date: Nov 2011
Posts: 1
leoromerbric is on a distinguished road
Default NSDictionary problem....

Hey everybody...I have a problem.

I used the XMLReader class to get a NSDictionary and everything works fine. But i can't to get the values of the atributes of my productData element.

Specifically i have the following NSDictionary:


{
response = {
products = {
productsData = (
{
alias = "Product 1";
id = 01;
price = "10";
},
{
alias = "Product 2";
id = 02;
price = "20";
},
{
alias = "Product 3";
id = 03;
price = "30";
}
);
};
};
}

I used this code to create de NSDictionary:

NSDictionary *dictionary = [XMLReader dictionaryForXMLData:responseData error:&parseError];

and responseData contains:

<application><products>
<productData>
<id>01</id>
<price>10</price>
<alias>Product 1</alias>
</productData>
<productData>
<id>02</id>
<price>20</price>
<alias>Product 2</alias>
</productData>
<productData>
<id>02</id>
<price>20</price>
<alias>Product 3</alias>
</productData>
</products>
</application>

Then, i don't know how get the values of each productData like id, price and alias...

Anybody know how do it??

Thanks and please forgive my bad english !
leoromerbric is offline   Reply With Quote
Reply

Bookmarks

Tags
nsdictionary, xml

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: 319
7 members and 312 guests
blueorb, givensur, guusleijsten, jbro, mer10, n00b, SLIC
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,113
Posts: 402,880
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

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