Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSXMLElement with solo tag . ie. <tagname attributename=attributevalue />

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 />
like image 461
Renjith Avatar asked Dec 20 '22 23:12

Renjith


1 Answers

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.

like image 187
yo6 Avatar answered May 11 '23 01:05

yo6