3

I have a problem.

I am using 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 to do it??

Thanks and please forgive my bad english !

Dilip Rajkumar
  • 7,006
  • 6
  • 60
  • 76
leoromerbric
  • 33
  • 1
  • 1
  • 3
  • Looks like you have nested dictionaries. Use [[dic objectForKey:@"response"]objectForKey:@"products"], the next one may be an array of dictionaries, I don't know, have some patience and NSLog each step. – Jano Nov 09 '11 at 23:34

3 Answers3

23
NSArray* keys = [theDict allKeys];

for(NSString* key in keys) {
    id obj = [theDict objectForKey:key];

    // do what needed with obj
}

you could try something like this :

NSArray* keys = [theDict allKeys];

for(NSString* key in keys) {
    if ([key isEqualToString:@"product"]) {
    NSArray* arr = [theDict objectForKey:key];

    // do what needed with arr 
}
    }
Oliver
  • 23,072
  • 33
  • 138
  • 230
  • 1
    Thank you for your answer :). Just note the `[key isEqualToString@"product"]` should be `[key isEqualToString:@"product"]` with **:** – Sawsan May 12 '13 at 07:28
4

There is a method on NSDictionary - -allValueswhich returns a new array containing the dictionary’s values. Maybe that will help.

CherryKuczery
  • 163
  • 2
  • 7
1

Starting with

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

you can do something like this:

NSDictionary *application = [dictionary objectForKey:@"application"];
if ([[application objectForKey:@"products"] isKindOfClass [NSArray class]]) {
    NSArray *products = [application objectForKey:@"products"];
    for (NSDictionary *aProduct in products) {
       // do something with the contents of the aProduct dictionary
    }
else if {[[application objectForKey:@"products"] isKindOfClass [NSDictionary class]]) {
    // you will have to see what the returned results look like if there is only one product 
    // that is not in an array, but something along these lines may be necessary
    // to get to a single product dictionary that is returned
}

I have had a case similar to this (parsing JSON) where an array was not returned for a signle value, so the result had to be checked for both an array (of product dictionaries in your case) or a single NSDictionary (the product dictionary in your case).

Jim
  • 5,940
  • 9
  • 44
  • 91
  • Thanks Jim...works perfectly! I didn't know that it would returned a NSDictionary when it was a single result, and it would returned NSArray (specifically a array of NSDictionarys ) when it was many results... Thanks again! – leoromerbric Nov 10 '11 at 17:11