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 />
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With