objective-cioskissxml

How to write CDATA with KissXML?


I am creating an application for iOS which needs to create an XML document. I do this via KissXML. Part of the XML looks like

<ISIN><![CDATA[12345678]]></ISIN>

I cannot find any option in KissXML to create the CDATA part. Simply adding a string with the CDATA stuff as text will result in escaping the special characters like < and >. Can anyone give me a hint in how to write CDATA with KissXML?


Solution

  • Even though the solution by @moq is ugly, it works. I have cleaned up the string creation code and added it into a category.

    DDXMLNode+CDATA.h:

    #import <Foundation/Foundation.h>
    #import "DDXMLNode.h"
    
    @interface DDXMLNode (CDATA)
    
    /**
     Creates a new XML element with an inner CDATA block
     <name><![CDATA[string]]></name>
     */
    + (id)cdataElementWithName:(NSString *)name stringValue:(NSString *)string;
    
    @end
    

    DDXMLNode+CDATA.m:

    #import "DDXMLNode+CDATA.h"
    #import "DDXMLElement.h"
    #import "DDXMLDocument.h"
    
    @implementation DDXMLNode (CDATA)
    
    + (id)cdataElementWithName:(NSString *)name stringValue:(NSString *)string
    {
        NSString* nodeString = [NSString stringWithFormat:@"<%@><![CDATA[%@]]></%@>", name, string, name];
        DDXMLElement* cdataNode = [[DDXMLDocument alloc] initWithXMLString:nodeString
                                                                   options:DDXMLDocumentXMLKind
                                                                     error:nil].rootElement;
        return [cdataNode copy];
    }
    
    @end
    

    The code is also available in this gist.