NSXMLElement with solo tag . ie. <tagname attributename=attributevalue />
macos, nsxml, nsxmldocument, nsxmlelement, objective-c
Solution
Before saving your file, when creating NSData, add this option : NSXMLNodeCompactEmptyElement
Like this :
NSData *xmlData =
[xmlDoc XMLDataWithOptions:NSXMLNodePrettyPrint | NSXMLNodeCompactEmptyElement];
Then serialize your file :
[xmlData writeToURL:theFile atomically:YES]
Reference : NSXMLNode class reference Section Constants
NSXMLNodeExpandEmptyElement Requests that an element should be expanded when empty; for example, <flag></flag>. This is the default. Available in Mac OS X v10.4 and later. Declared in NSXMLNodeOptions.h.
NSXMLNodeCompactEmptyElement Requests that an element should be contracted when empty; for example, <flag/>. Available in Mac OS X v10.4 and later. Declared in NSXMLNodeOptions.h.
Problem
I'm trying to create a XML string which should contain solo tags (ie. tag without separate closing tags) I am able to create normal xml tags like ``` <tagname attribute=attributevalue></tagname> ``` using the code ``` NSXMLElement *childElement=[[NSXMLElement alloc] initWithName:childName]; childElement.stringValue=childValue; [childElement addAttribute:[NSXMLNode attributeWithName:attributeName stringValue:attributeValue]]; [self.currentNode addChild:[childElement copy]]; ``` but I need it like this ``` <tagname attributename=attributevalue /> ```