-1

I would like to loop recursively over DDXMLDocument, and to change the elements attributes.

How can I do it ? I currently have the document and the root element:

DDXMLDocument *theDocument = [[DDXMLDocument alloc] initWithXMLString:content options:0 error:&error];
    DDXMLElement *rootElement = theDocument.rootElement;
Dejell
  • 13,947
  • 40
  • 146
  • 229

1 Answers1

1

postfix tree walk implemented:

-(void)processNode:(DDXMLNode *)node {
    if(node.kind == DDXMLElementKind) {
       //...
       for(DDXMLNode *child in node.children) {
           [self processNode:child];
       }
    }
}
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
  • It should be like this: -(void)processNode: (DDXMLNode *) node – Dejell Dec 31 '12 at 17:57
  • Thanks. BTW, if I send the DDXMLDocument as the first root to the method it also parses the . How can I send it without this element? – Dejell Dec 31 '12 at 20:58
  • There should be a node type element and actually you should check that in processNode using an if – Daij-Djan Jan 01 '13 at 01:05
  • I still have a problem. What if my XML looks like this? <[CDATA[ ]]> ? the amount of found children is only 1 - the HTML it can't omit the CDATA.. I need the CDATA since I have &nsbp in one of the div's content – Dejell Jan 01 '13 at 11:27
  • .. thats whats CDATA is for -- CDATA is not parsed. (Just last week I answered that on SO .. you have collect the NSData and parse using another instance of the parser) http://stackoverflow.com/questions/14070750/cdata-not-being-parsed-with-nsxmlparser/14072073#14072073 (this applies to NSXMLParser but the idea is the same) – Daij-Djan Jan 01 '13 at 12:39
  • `NSString *cData = [childNode stringValue];` -- feed that into a XML parser – Daij-Djan Jan 01 '13 at 12:41
  • I didn't understand the last comment - what can I do in case of having &nsbp? – Dejell Jan 01 '13 at 12:42
  • get all the cdata as one huge string and feed that as new content into a new parser – Daij-Djan Jan 01 '13 at 12:47
  • theDocument stringValue is null ! – Dejell Jan 01 '13 at 13:47