使用xml-simple解析<first_name>João</first_name>

时间:2010-09-02 21:38:26

标签: ruby xml xml-simple

我在rake任务中使用xml-simple gem来解析db转储的内容。问题是数据库xml文件包含标题中导致xml-simple崩溃的字符。有没有解决这个问题?

1 个答案:

答案 0 :(得分:2)

Nokogiri似乎有效:

require 'nokogiri'

xml =<<ENDOFxML
  <test>
    <first_name>João</first_name>
  </test>
ENDOFxML

doc = Nokogiri::XML.parse(xml)
doc.xpath('//first_name').each do |node|
  puts node.inner_text
end

#Output: João