Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a cdata block using GDataXMLNode?

Tags:

xml

iphone

I've tried this:

GDataXMLElement * body = [GDataXMLNode elementWithName:@"body"];
[body addChild:[GDataXMLNode elementWithName:@"request" stringValue:@"<![CDATA[ <hello> ]]>"]];
NSLog(@"%@",[body XMLString]);

And it outputs:

<body><request>&lt;![CDATA[&lt;hello&gt; ]]&gt;</request></body`>

But want it to be like this:

<body><request><![CDATA[ <hello> ]]></request></body>

any idea how can I tell the parser that the GDataXMLNode should be a CDATA kind?

like image 756
Fede Mika Avatar asked Oct 13 '10 02:10

Fede Mika


People also ask

How do I add CDATA to XML?

CDATA sections can appear inside element content and allow < and & character literals to appear. A CDATA section begins with the character sequence <! [CDATA[ and ends with the character sequence ]]>. Between the two character sequences, an XML processor ignores all markup characters such as <, >, and &.

What is a CDATA block?

CDATA is defined as blocks of text that are not parsed by the parser, but are otherwise recognized as markup. The predefined entities such as &lt;, &gt;, and &amp; require typing and are generally difficult to read in the markup.

What is the use of CDATA block in this XML document?

A CDATA section is used to mark a section of an XML document, so that the XML parser interprets it only as character data, and not as markup. It comes handy when one XML data need to be embedded within another XML document.


1 Answers

Did you trying using:

[[GDataXMLElement alloc] initWithXMLString:"<![CDATA[ .... ]]>"]

Baseed on the source this parses it directly as XML:

const char *utf8Str = [str UTF8String];
xmlDocPtr doc = xmlReadMemory(utf8Str, (int)strlen(utf8Str), NULL, // URL
                              NULL, // encoding
                              kGDataXMLParseOptions);

Whereas elementWithName just grabs the string verbatim:

+ (GDataXMLElement *)elementWithName:(NSString *)name {
    xmlNodePtr theNewNode = xmlNewNode(NULL, // namespace
                                       GDataGetXMLString(name));
    if (theNewNode) {
    // succeeded
    return [self nodeConsumingXMLNode:theNewNode];
}
return nil;
like image 153
AdamH Avatar answered Oct 30 '22 21:10

AdamH