PDA

View Full Version : XML attributes and reading what they are




Chirone
Jul 29, 2009, 07:58 PM
ok, this is going to sound really stupid...

i can add an attribute to a node easily enough and append it to another node


NSXMLNode* attr = [[NSXMLNode alloc] initWithKind: NSXMLAttributeKind];
[attr setName: @"testAttr"];
[attr setStringValue: @"testVal"];
[root addChild: [NSXMLNode elementWithName: @"testNode" children: nil attributes: [NSArray arrayWithObject: attr]]];


easy, and it appears as i'd expect

now... how do i read that value back (specifically from a file, not when i just created it)?
I saw that you can get/set attribute values for an NSXMLElement which is a subclass of NSXMLNode

so how do i get the attributes of an NSXMLNode?

eeeh, nevermind, i missed the part where NSXMLDocument returns an NSXMLElement when you call rootNode

oh wait, that's right... NodesForXPath returns an NSArray of NSXMLNode

is it possible to get the attribute name and value from an NSXMLNode?


pfff, i knew this was a stupid question! just needed a few minutes to experiment more

found me what i was after

NSXMLNode* rootTwo = [xmlDoc rootElement];
NSArray* pathNodes = [rootTwo nodesForXPath:@"/singQToRenderQ/testNode" error: nil];
NSXMLElement* elementNode = [pathNodes objectAtIndex: 0];
NSArray* attributesFromElement = [elementNode attributes];
NSXMLNode* theAttributeNode = [elementNode attributeForName: @"testAttr"];