开发者

Using NSXMLParsers "found CDATA" method

开发者 https://www.devze.com 2023-01-23 00:53 出处:网络
I have an XML file that looks like this <comments> <text> <![CDATA[ <!--cached-Tue, 02 Nov 2010 17:50:50 +0000-->

I have an XML file that looks like this

<comments>
    <text>
          <![CDATA[
              <!--cached-Tue, 02 Nov 2010 17:50:50 +0000-->
              this is on the first line
              <br /> 
              This is on the second line
           ]]>  
    </text> 
    <text>
          <![CDATA[
              <!--cached-Tue, 02 Nov 2010 17:50:50 +0000-->
              this is on the first line
              <br /> 
              This is on the second line
           ]]>  
    </text> 
</comments>

How do I extract the data out of that (ignoring the comment)

I currently have.

- (void)parser:(NSXMLParser *)parser foundCDAT开发者_如何学PythonA:(NSData *)CDATABlock 
   {    
    if(!myCDATA)
            {
            myCDATA = [[NSMutableData alloc] init];
            }   
    [myCDATA appendData:CDATABlock];
    }

However, it never seems to find the "]]>" tag. When I break point it only calls to foundCDATA once.


Are you setting that breakpoint on the line below by any chance?

myCDATA = [[NSMutableData alloc] init];

Just add a NSLog(@"") before your if(!myCDATA) test and set a breakpoint on it and I am pretty sure you'll get two callbacks.

You should not expect the parser to "find" the "]]>" string. That is part of the CDATA element.

You could also implement this

- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName

and check for "text".

Cheers...

0

精彩评论

暂无评论...
验证码 换一张
取 消