Search code examples
iosparsingxml-parsingnsxmlgdataxml

Quotations marks lost when saving xml with GDataXml


I have a xml file like this:

<Patients>
<patient name="someName" lastName="someLastName">
<aProperty>anIntegerValue</aProperty>
</patient>
</Patients>

So I parse this with NSXMLParser like this:

In didStartElement I have something like:

if ([elementName isEqual:@"Patients"]) {
appDelegate.patients = [[NSMutableArray alloc] init];
}
else if ([elementName isEqualToString:@"patient"]){
    aPatient = [[Patient alloc] init];
    aPatient.name = [attributeDict objectForKey:@"name"];
    aPatient.lastName = [attributeDict objectForKey:@"lastName"];

Then at some point I want to save the changes made, so I use GDataXML to save it like this:

GDataXMLElement * patientsElement = [GDataXMLNode elementWithName:@"Patients"];
for(Patient *aPatient in patients) {
    GDataXMLElement *nameElement = [GDataXMLNode elementWithName:@"name"];
            GDataXMLElement *lastNameElement = [GDataXMLNode elementWithName:@"lastName"];
[aPatient addChild:nameElement];
[aPatient addChild:lastNameElement];

Etc

After I do this the xml file looks like this:

<Patients>
<patient>
<name>someName</name> 
<lastName>someLastName</lastName>
<aProperty>anIntegerValue</aProperty>
</patient>
</Patients>

(Notice the quotation marks are gone) So when I want to parse this file I get all "null" values. How do I add the quotation marks there? Or just avoid this error.


Solution

  • Looks like you are getting null values when re-parsing because you no longer have name & last name as attributes, but now have them as elements. When creating aPatient element do not addChild instead addAttribute or equivalent for GDataXML