The parsing of the XML is simply done in Xcode, regardless of wether you are using Cocos2D or not.
When you use Inkscape to create your object, look at the object properties. You then set a value against the LEVEL label (example #brick, #tree, #car, etc) to create the object you desire.
When the XML is parsed, examine the value for key inkscape:label. When you have this, then the properties that follow relate to that object.
Here is an cut-down version of what I do to parse the XML from the SVG file to create my objects.
-(void) parseXML {
// Parse the SVG level
int lvlNo = 100 + [GameManager sharedGameManager].activeLevel;
NSLog(@"SVG Level Builder Initiated.");
for (int i=lvlNo; i<lvlNo+1; i++) {
NSString *filePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"level%i",i] ofType:@"svg"];
NSXMLParser *parser;
parser = [[NSXMLParser alloc] initWithData:[NSData dataWithContentsOfFile:filePath]];
[parser setDelegate:self];
[parser parse];
}
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
// Parsing the SVG
NSString *transform;
if ([elementName isEqualToString:@"g"]) {// Get the document transformation values
transform = [attributeDict valueForKey:@"transform"];
NSString *transformString = [transform substringToIndex:[transform length] - 1];
transformString = [transformString substringFromIndex:10];
} else if ([elementName isEqualToString:@"image"]) {// Get the Images
NSString *type = [attributeDict valueForKey:@"inkscape:label"];
NSString *numType = [type substringFromIndex:1];
int tln = [numType intValue]; // Object type
float x = [[attributeDict valueForKey:@"x"] floatValue];
float y = [[attributeDict valueForKey:@"y"] floatValue];
float width = [[attributeDict valueForKey:@"width"] floatValue];
float height = [[attributeDict valueForKey:@"height"] floatValue];
// Calculate centre of image
int iX = (x + width/2);
int iY = y - (height/2);
[self createLayerObject:tln withX:iX withY:iY withZ:9]; // This is the method called to create my object
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
//
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
//
}
- (void) parserDidEndDocument:(NSXMLParser *)parser{
NSLog(@"SVG Level Builder Terminated.");
}
When you create your game object, create it as you would normally within your game. If you are not using any physics, etc, then you may just simply be adding sprites etc as child items to the layer.
You also need to make sure that you make reference to the correct delegate methods for your layer.
@interface GameLayer () <NSXMLParserDelegate>
Hope that helps.
Tony B