Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How to convert XML to dictionary

ashwanik04ashwanik04 Posts: 16Registered Users
edited November 2011 in iOS SDK Development
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 :)
Post edited by ashwanik04 on
·

Replies

  • smithdale87smithdale87 Posts: 4,447iPhone Dev SDK Supporter @ @ @ @ @
    edited April 2009
    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.
    ·
  • ashwanik04ashwanik04 Posts: 16Registered Users
    edited April 2009
    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 :(
    ·
  • smithdale87smithdale87 Posts: 4,447iPhone Dev SDK Supporter @ @ @ @ @
    edited April 2009
    can you post your code, and then we can discuss what you are doing wrong, or possibly a different approach?
    ·
  • epicjorgeepicjorge Posts: 2New Users
    edited April 2009
    If you can get your xml data in plist format it would be much easier to do.
    ·
  • KalimbaKalimba ¿La Islas Hermosas?Posts: 2,181iPhone Dev SDK Supporter, Registered Users
    edited April 2009
    ashwanik04 wrote: »
    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.
    <a href="http://bit.ly/gAmufK" target="_blank">icon-r-64.png</a> ~~ Word Flurry ~~ <a href="http://itunes.apple.com/us/app/word-flurry/id399660201?mt=8" target="_blank">App Store</a> / <a href="http://bit.ly/bJiK99
    ·
  • vineeth_cvineeth_c Posts: 7New Users
    edited August 2009
    use tinyxml to parse dictionary. this is a light weight c++ library..:)
    ·
  • kelvinkaokelvinkao Posts: 352Registered Users
    edited August 2009
  • Benoit CaccinoloBenoit Caccinolo Posts: 5New Users
    edited December 2010
    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,
    ·
  • CœurCœur Posts: 5New Users
    edited June 2011
    improved code of bcaccinolo's code:
    https://github.com/Insert-Witty-Name/XML-to-NSDictionary

    You simply do:
    NSDictionary *dic = [XMLReader dictionaryForXMLString:XML error:nil];
    

    You get:
    {
        Grandfather =     {
            Son =         (
                            {
                    Grandson =                 {
                        name = 1;
                    };
                    name = 1;
                },
                            {
                    Grandson =                 {
                        name = 1;
                    };
                    name = 2;
                },
                            {
                    Grandson =                 (
                                            {
                            name = 1;
                        },
                                            {
                            name = 2;
                        },
                                            {
                            name = 3;
                        }
                    );
                    name = 3;
                }
            );
        };
    }
    
    ·
  • leoromerbricleoromerbric Posts: 1New Users
    edited November 2011
    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 :D!
    ·
Sign In or Register to comment.