在iPad中解析XML文件时如何处理CDATA标记?

时间:2022-12-01 13:20:12

I am working on an application where I need to parse some XML files that consists CDATA tags. Parsing ordinary xml is quite straight forward but I am facing problems to retrieve data that is inside the CDATA tag.

我正在开发一个应用程序,需要解析包含CDATA标记的一些XML文件。解析普通xml非常直接,但是我在检索CDATA标记中的数据时遇到了问题。

The parser:foundCDATA: method is being called for each CDATA tag encountered where the parameter CDATABlock is of NSData type.

对于遇到参数为NSData类型的CDATABlock的每个CDATA标记,都会调用解析器:foundCDATA:方法。

Please suggest a way to parse the CDATA tag.

请建议一种解析CDATA标记的方法。

2 个解决方案

#1


5  

If you need to extract the string from CDATA, you could use this block in foundCDATA:

如果需要从CDATA中提取字符串,可以使用foundCDATA中的这个块:

NSMutableString *lStr = [[NSMutableString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding];

#2


0  

i have taken string from the CDATA tag as Oleg Danu said but still it consists XML tags.So i wrote that entire string to a file,whenever CDATA found, and created one more XMLParser by setting the delegate to same class as that of original Parser.All tags in CDATA tag are parsed properly by the secondary parser.The parser will be released at the end of the foundCDATA method and actual parsing continues as it is.

正如Oleg Danu所说,我从CDATA标记中提取了字符串,但它仍然包含XML标记。因此,每当CDATA找到时,我都会将整个字符串写到一个文件中,并通过将委托设置为与原始解析器相同的类来创建一个XMLParser。CDATA标记中的所有标记都由辅助解析器正确解析。解析器将在foundCDATA方法的末尾释放,实际解析将继续进行。

#1


5  

If you need to extract the string from CDATA, you could use this block in foundCDATA:

如果需要从CDATA中提取字符串,可以使用foundCDATA中的这个块:

NSMutableString *lStr = [[NSMutableString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding];

#2


0  

i have taken string from the CDATA tag as Oleg Danu said but still it consists XML tags.So i wrote that entire string to a file,whenever CDATA found, and created one more XMLParser by setting the delegate to same class as that of original Parser.All tags in CDATA tag are parsed properly by the secondary parser.The parser will be released at the end of the foundCDATA method and actual parsing continues as it is.

正如Oleg Danu所说,我从CDATA标记中提取了字符串,但它仍然包含XML标记。因此,每当CDATA找到时,我都会将整个字符串写到一个文件中,并通过将委托设置为与原始解析器相同的类来创建一个XMLParser。CDATA标记中的所有标记都由辅助解析器正确解析。解析器将在foundCDATA方法的末尾释放,实际解析将继续进行。