解析XML数据或转换为NSDictionary?

时间:2012-01-22 04:58:16

标签: iphone xcode parsing nsarray nsdictionary

我有从服务器检索的xml数据,我需要删除它的一些部分才能设置我的tableview。我正在将这些数据转换为NSDictionary,但我认为解析(是正确的词?)xml数据可能更容易,而不是在它已经转换后弄乱它。这是xml数据的一个示例。

 <?xml version="1.0" encoding="UTF-8"?>
 <response status="ok">
 <rosters records_per_page="50" page_count="1" page="1" total_record_count="11">
 <roster class_id="685343" role="T" user_id="968428" roster_id="16257539">
 <class name="Haiku API Keys" shortname="haiku_api" end_date="2011-12-20" code=""      
 import_id="" id="685343" description="" teacher_id="971456" year="2012" 
 organization_id="134146" active="false"/>
 </roster>
 <roster class_id="512296" role="S" user_id="968428" roster_id="10456326">
 <class name="Physics AP" shortname="physics-ap_0406-01" end_date="2012-05-02" 
 code="PHYSICSA_0406-01" import_id="0406-01-2012" id="512296" description="DESCRIPTION" 
 teacher_id="968968" year="2012" organization_id="134148" active="true"/>
 </roster>

我想要做的是删除对象为“active”的对象为“false”的数据。这将留下具有物理AP的类名的数据。这可能与xml数据有关,还是我应该尝试在NSDictionary之后删除这些数据?这就是转换后的NSDictionary的样子。

 (
 {
response =     {
    "@status" = ok;
    rosters =         {
        "@page" = 1;
        "@page_count" = 1;
        "@records_per_page" = 50;
        "@total_record_count" = 11;
        roster =             (
                            {
                "@class_id" = 685343;
                "@role" = T;
                "@roster_id" = 16257539;
                "@user_id" = 968428;
                class =                     {
                    "@active" = false;
                    "@code" = "";
                    "@description" = "";
                    "@end_date" = "2011-12-20";
                    "@id" = 685343;
                    "@import_id" = "";
                    "@name" = "Haiku API Keys";
                    "@organization_id" = 134146;
                    "@shortname" = "haiku_api";
                    "@teacher_id" = 971456;
                    "@year" = 2012;
                };
            },
                            {
                "@class_id" = 512296;
                "@role" = S;
                "@roster_id" = 10456326;
                "@user_id" = 968428;
                class =                     {
                    "@active" = true;
                    "@code" = "PHYSICSA_0406-01";
                    "@description" = "DESCRIPTION";
                    "@end_date" = "2012-05-02";
                    "@id" = 512296;
                    "@import_id" = "0406-01-2012";
                    "@name" = "Physics AP";
                    "@organization_id" = 134148;
                    "@shortname" = "physics-ap_0406-01";
                    "@teacher_id" = 968968;
                    "@year" = 2012;
                };
            },
            }

正如xml数据的格式化方式让我觉得通过解析它会更容易实现,但我不知道它是否可行,或者使用什么方法。如果您有办法通过使用xml数据或NSDictionary来实现这一点,我很乐意听到它。谢谢你的帮助。

2 个答案:

答案 0 :(得分:0)

如果您不熟悉目标c中的xml解析,我建议您使用TBXML(http://tbxml.co.uk/)库。使用此库解析您的xml并在Array中存储您想要的内容或字典(留下你不想要的东西)..并使用数组或字典来创建你的表..

答案 1 :(得分:0)

我使用你使用的相同的XMLReader,我喜欢它。

如果我要解决问题,这是我的解决方案,在你得到字典后,你只需要创建一个新的字典数组,具体来说就是一个新的名单数组,如:

NSArray *roster = [[[theDictionary objectForKey:@"response"] objectForKey:@"rosters"] objectForKey:@"roster"];

NSMutableArray *newRoster = [[NSMutableArray alloc] init];

for(int i=0;i<[roster count];i++){
    NSDictionary *aRoster = [roster objectAtIndex:i];
    //filter the array and add it to the mutable array
    if([[[aRoster objectForKey:@"class"] objectForKey:@"@@active"] isEqualToString:@"true"])
    {
        //this way we only add those data whose active is "true"
        [newRoster addObject:aRoster];
    }
}

//do what you want with the new roster
NSLog(@"newRoster = %@",newRoster);

在这种情况下我只是假设您只使用“名册”标签数组,并且您想删除那些“活动”属性等于字符串“false”的标签。

完成后,不要忘记释放newRoster。

当涉及到属性时,XMLReader非常繁琐,如果我有一些错误,请纠正我,因为我今天没有使用mac