我試圖解析一個大的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