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. 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. Here’s a sample of the xml data.
<?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.” This would leave me the data with the class name of Physics AP. Is this possible to do with xml data or should I try to remove this data once it is an NSDictionary? This is what the converted NSDictionary looks like.
(
{
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. If you have a way to accomplish this by using the xml data or the NSDictionary I would love to hear it. Thanks for your help.
I use the same XMLReader you’ve used, and I like it.
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:
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”.
Don’t forget to release the newRoster when you are done with it.
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