我试图解析一个大的XML文件来获取所有外部XML标签内容,这样的事情:如何使用SAX获取CDATA内容
<string name="key"><![CDATA[Hey I'm a tag with & and other characters]]></string>
得到这个:
<![CDATA[Hey I'm a tag with & and other characters]]>
虽然,当我使用引入nokogiri的SAX XML解析器我只得到了文本而不CDATA与转义字符,像这样:
Hey I\'m a tag with & and other characters
这为m y代码:
class IDCollector < Nokogiri::XML::SAX::Document
def initialize
end
def characters string
puts string # this does not works, CDATA tag is not printed
end
def cdata_block string
puts string
puts "<![CDATA[" + string + "]]>"
end
end
有没有什么办法可以与Nokogiri SAX做到这一点?
这并不完全清楚你想要做什么:读取或生成CDATA块?你不会得到'<![CDATA [嘿,我是带有&和其他字符的标签]]>'因为它是一个块,而不是标签或元素。 '<![CDATA ['实际上就是标签,但它被处理完了,只有它的内容被返回。 http://stackoverflow.com/q/2784183可能会有所帮助。我无法重复获取编码结果。 –
我的最终目标是将一些带有内部内容的xml标签移植到其他文件中。虽然,这两个文件很大,我必须使用SAX或其他我有一个内存异常 – iGoDa