使用TBXML将XML解析为Array iOS

时间:2013-11-04 11:54:26

标签: ios iphone objective-c xml

我的XML字符串如下所示:

result = { 
         node = ( { id = { text = 27; }; 
         img = { text = "/upload/iblock/1a1/1a84f.png"; }; 
         name = { text = rtrttrrt; }; price = { text = 0; }; 
         type = { text = 0; }; }, { id = { text = 28; }; 
         img = { text = "e.png"; }; name = { text = gdd; }; 
         price = { text = 0; }; 
         type = { text = 0; }; }, 
        { id = { text = 30; }; 
        img = { text = "9.png"; }; 
        name = { text = gfdghfdh; }; 
        price = { text = 0; }; 
        type = { text = 0; }; }, 
        { id = { text = 29; };
        img = { text = "8.png"; }; 
       name = { text = d; }; 
       price = { text = 0; }; 
       type = { text = 0; }; 
     } ); }; }

我正在解析它并获得NSDictionary(称为xmlDictionary),如下所示:

result = `{ content = ( { text = "iPhone - shit"; }, { text = "iPad = cool"; } ); date = ( { text = "4 december"; }, { text = "16 december"; } ); title = ( { text = "News about iPhone"; }, { text = "News about iPad"; } ); type = ( { text = "News 1"; }, { text = "News 2"; } ); }; }

我如何获得每个元素的属性,例如标题文本?我在尝试:

NSString *str =[[[[xmlDictionary objectForKey:@"result"] objectForKey:@"type"] objectForKey:@"title"] stringForKey:@"text"]; 

但它不起作用。我该怎么办?

1 个答案:

答案 0 :(得分:0)

你可以

NSArray *titleArray =[[[xmlDictionary objectForKey:@"result"] objectForKey:@"title"] valueForKey:@"text"];
NSString *str = [titleArray objectAtIndex:(0 or 1)];

编辑: 您的json结构顺序不正确。如下所示,

result={
    node=({
        id={
            text=27;
        };img={
            text="acc8e84f.png";
        };name={
            text=rtrttrrt;
        };price={
            text=0;
        };type={
            text=0;
        };
    }

请检查您的回复并告诉我们。

编辑2:

这样做,

NSArray *nameArray = [[[[xmlDictionary objectForKey:@"result"] objectForKey:@"node"]  valueForKey:@"name"] valueForKey:@"text"];