0

I have got an object serialization from Java into a XML file that I have to deserialize for an iOS App using objective-c.

I am using NSXMLParser for the first time and have a given object structure in objective-c.

Here is a sample of the XML file:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<Content.Project>
  <versionName>0.5.241</versionName>
  <spriteList>
    <Content.Sprite>
      <costumeDataList>
        <Common.CostumeData>
          <costumeFileName>CCE87ECA859D4448BC47A837CF024464_background</costumeFileName>
          <costumeName>background</costumeName>
        </Common.CostumeData>
      </costumeDataList>
      <soundList/>
      <scriptList>
        <Content.StartScript>
          <brickList>
            <Bricks.SetCostumeBrick>
              <costumeData reference="../../../../../costumeDataList/Common.CostumeData"/>
              <sprite reference="../../../../.."/>
            </Bricks.SetCostumeBrick>
          </brickList>
          <sprite reference="../../.."/>
          <name>stageStartScript</name>
        </Content.StartScript>
      </scriptList>
      <name>Background</name>
    </Content.Sprite>
...

Then I have given classes (derived from NSObject) for certain sections of the XML, such as a Level that contains an array of Sprites and the Sprite class in turn contains an array of Costumes and so on...

My problem is that the XMLParser does not really 'know' to which class the value corresponds. I have to differ between the different classes to set the corresponding properties.

For example, the following line works for all properties of the Level object (the first level of <Content.Project> but not <spriteList> and so on): [self.level setValue:self.currentElementValue forKey:elementName];

So basically my question is: Is there a way to distinguish between the different 'levels' or 'depth' in an XML file in the...

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 

... method of the parser?

I am sure there must be an easy way to achieve this but I have not found one on google. Thanks in advance.

Chris
  • 3,057
  • 5
  • 37
  • 63

1 Answers1

0

Make a depth counter. Increase it in didStartElement, and decrease it in didEndElement :-)

Christian Stieber
  • 9,954
  • 24
  • 23
  • This was also my first idea, but due to the fact that the xml is not standardized and another team is working on it there can still be any changes so I am looking for a generic solution. – Chris Jul 15 '12 at 20:08