Xcode xml解析问题

时间:2012-04-08 09:55:44

标签: iphone xml xcode xcode4 xml-parsing

我在Xcode中创建了一个解析xml的应用程序,但我在解析这段代码时遇到了问题

<description>
<![CDATA[
<object type="application/x-shockwave-flash" data="http://www.youtube.com/v/Sg8z_On_Jmc?fs=1" width="580" height="25"> <param name="movie" value="http://www.youtube.com/v/Sg8z_On_Jmc?fs=1" /> <param name="allowFullScreen" value="true" /> </object>
]]>
</description>

如何获取数据或值中的URL?

感谢。

我的源代码:.M

//在开头的didStartElement;

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
{

    if (![self currentStringValueTitle]) {

        [self setCurrentStringValueTitle:[[NSMutableString alloc] initWithCapacity:50]];

    }

    if (![self currentStringValueImage]) {

        [self setCurrentStringValueImage:[[NSMutableString alloc] initWithCapacity:50]];

    }


    if (![self currentStringValueDescription]) {

        [self setCurrentStringValueDescription:[[NSMutableString alloc] initWithCapacity:50]];

    }


    [[self currentStringValueTitle] appendString:string];
    [[self currentStringValueDescription] appendString:string];
    [[self currentStringValueImage] appendString:string];

    NSLog(@"Başlık: %@", [self currentStringValueTitle]);
    NSLog(@"Detay: %@", [self currentStringValueDescription]);

}


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

   if ( [elementName isEqualToString:@"title"] ) {

        [[self titleArray] addObject:[self currentStringValueTitle]];


    }
   if ( [elementName isEqualToString:@"pubDate"] ) {


        [[self descriptionArray] addObject:[self currentStringValueDescription]];


    }

    if ( [elementName isEqualToString:@"description"] ) {

        [[self imageArray] addObject:[self currentStringValueImage]];
    } 


    [self setCurrentStringValueTitle:nil]; 
    [self setCurrentStringValueDescription:nil];
    [self setCurrentStringValueImage:nil]; 


}

1 个答案:

答案 0 :(得分:0)

使用CDATA背后的想法是,无论内在的是什么,它都会被解析器忽略......但幸好NSXMLParser可以替代它...

- (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock
{
    NSString *someString = [[NSString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding];
}

现在someString是你的另一个xml字符串,你可以使用另一个NSXMLParser实例解析(这是一个XML)...希望这会有所帮助。