2010-10-13 9 views
6

Ho provato questo:Come posso aggiungere un blocco cdata usando GDataXMLNode?

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

che emette:

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

Ma vuoi che sia così:

<body><request> <! [CDATA [<ciao>]] > </richiesta > < /body>

qualche idea di come posso dire il parser che il GDataXMLNode dovrebbe essere una sorta CDATA?

+0

mai trovare fuori? Mi piacerebbe saperlo anche io. – ransomweaver

+0

Inoltre, il CDATA viene eliminato completamente: GDataXMLElement * n = [[GDataXMLElement alloc] initWithXMLString: @ "" errore: nil]; – ransomweaver

+0

quindi ottieni mystring ransomweaver

risposta

2

Forse voi cercando utilizzando:

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

Baseed sulla sorgente questo analizza direttamente in formato XML:

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

Mentre elementWithName appena afferra la stringa verbatim:

+ (GDataXMLElement *)elementWithName:(NSString *)name { 
    xmlNodePtr theNewNode = xmlNewNode(NULL, // namespace 
             GDataGetXMLString(name)); 
    if (theNewNode) { 
    // succeeded 
    return [self nodeConsumingXMLNode:theNewNode]; 
} 
return nil; 
1

Benchè questa discussione è vecchia, ho pensato che potesse essere d'aiuto per qualcuno che si imbattesse in questo.

GDataXML non ha il supporto per i CData implementati. Ma sembra facile aggiungere questa funzionalità. GDataXML utilizza libxml2 per creare un documento XML. Nella sezione CD2 libxml2 potrebbe essere creato come suggerito in how to write a CDATA node using libxml2?.

Quindi, potremmo migliorare

+ (GDataXMLElement *)elementWithName:(NSString *)name stringValue:(NSString *)value

metodo GDataXMLNode di avere un parametro booleano che indica se abbiamo bisogno nodo DatiC e modificare la linea

xmlNodePtr textNode = xmlNewText(GDataGetXMLString(value)); 

per creare DatiC blocco invece di un blocco di testo

1

Aggiungere quanto segue a GDataXMLNode.h

+ (GDataXMLElement *)elementWithName:(NSString *)name cDataStringValue:(NSString *)value; 
- (void)setCDataStringValue:(NSString *)value; 

Aggiungere il seguente alla GDataXMLNode.m

+ (GDataXMLElement *)elementWithName:(NSString *)name cDataStringValue:(NSString *)value { 

    xmlNodePtr theNewNode = xmlNewNode(NULL, // namespace 
             GDataGetXMLString(name)); 
    if (theNewNode) { 

     NSUInteger length = [value lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; 

     xmlNodePtr textNode = xmlNewCDataBlock(theNewNode->doc, GDataGetXMLString(value), length); 

     if (textNode) { 

      xmlNodePtr temp = xmlAddChild(theNewNode, textNode); 
      if (temp) { 
       // succeeded 
       return [self nodeConsumingXMLNode:theNewNode]; 
      } 
     } 

     // failed; free the node and any children 
     xmlFreeNode(theNewNode); 
    } 
    return nil; 
} 

- (void)setCDataStringValue:(NSString *)value { 

    if (xmlNode_ != NULL && value != nil) { 

     NSUInteger length = [value lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; 

     xmlNodePtr textNode = xmlNewCDataBlock(xmlNode_->doc, GDataGetXMLString(value), length); 

     if (textNode) { 

      xmlAddChild(xmlNode_, textNode); 
     } 
    } 
} 

Non è necessario il metodo setCDataStringValue per questo esempio, ma ho incluso lo stesso, in modo da poter eventualmente creare il GDataXMLNode e aggiungere il DatiC tardi.

tuo esempio potrebbe quindi diventare:

GDataXMLElement * body = [GDataXMLNode elementWithName:@"body"]; 
GDataXMLElement * response = [GDataXMLNode elementWithName:@"response" cDataStringValue:@"<hello>"]; 
[body addChild:response]; 
NSLog(@"%@",[body XMLString]); 

quali uscite:

<body><response><![CDATA[<hello>]]></response></body> 
Problemi correlati