简体   繁体   English

解析XML数据或转换为NSDictionary?

[英]Parsing XML Data or convert to NSDictionary?

I have xml data that is retrieved from a server and I need to remove some parts of it in order to set up my tableview. 我有从服务器检索到的xml数据,我需要删除它的某些部分才能设置表格视图。 I am converting this data to an NSDictionary, but I think it might be easier to parse (is that the right word?) the xml data rather than messing with it once it's already converted. 我正在将这些数据转换为NSDictionary,但是我认为解析xml数据可能更容易(是正确的词吗?),而不是一旦转换后就将其弄乱。 Here's a sample of the xml data. 这是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>

What I want to do is remove the data that has the object "false" for the key "active." 我想做的是删除键“ active”的对象为“ false”的数据。 This would leave me the data with the class name of Physics AP. 这将使我的数据保留为Physics AP的类名。 Is this possible to do with xml data or should I try to remove this data once it is an NSDictionary? 这可能与xml数据有关吗,或者一旦它成为NSDictionary,我应该尝试删除此数据吗? This is what the converted NSDictionary looks like. 这就是转换后的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;
                };
            },
            }

Just the way the xml data is formatted makes me think it would be easier to accomplish by parsing it, but I don't know if it is possible, or what method to use. 正是xml数据的格式化方式使我认为通过解析它会更容易完成,但是我不知道是否有可能或使用哪种方法。 If you have a way to accomplish this by using the xml data or the NSDictionary I would love to hear it. 如果您有使用xml数据或NSDictionary完成此操作的方法,我希望听到它。 Thanks for your help. 谢谢你的帮助。

如果您不熟悉目标c中的xml解析,建议您使用TBXML(http://tbxml.co.uk/)库。使用此库解析xml并将所需的内容存储在Array或字典中(请保留不需要的内容),然后使用数组或字典创建表。

I use the same XMLReader you've used, and I like it. 我使用与您使用的相同的XMLReader,并且我喜欢它。

If I were to solve the problem, here's my solution, after you've got the dictionary you just need to create a new array of dictionary, to be specific a new array of roster, like this: 如果我要解决问题,这是我的解决方案,在获得字典后,您只需要创建一个新的字典数组即可,具体来说是一个新的花名册数组,如下所示:

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);

In this case im just assuming you only going to use the the array of "roster" tags and you want to remove those tags whose "active" attributes is equal to string "false". 在这种情况下,我只是假设您只使用“花名册”标签的数组,并且要删除那些“活动”属性等于字符串“ false”的标签。

Don't forget to release the newRoster when you are done with it. 完成操作后,别忘了发布newRoster。

The XMLReader is quite tedious to work with when it comes to attributes, correct me if I have some errors since i dont have a mac to use today XMLReader在处理属性时非常繁琐,如果我遇到一些错误,请改正我,因为我今天没有使用Mac

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM