尝试用rexml来解析xml文档的时候出现问题:
undefined local variable or method 'transitive' in document.rb
于是乎查看rexml的document.rb文件,大概在182行附近有一个方法:
def write( output=$stdout, indent=-1, trans=false, ie_hack=false )
if xml_decl.encoding != "UTF-8" && !output.kind_of?(Output)
output = Output.new( output, xml_decl.encoding )
end
formatter = if indent > -1
if transitive
REXML::Formatters::Transitive.new( indent, ie_hack )
else
REXML::Formatters::Pretty.new( indent, ie_hack )
end
else
REXML::Formatters::Default.new( ie_hack )
end
formatter.write( self, output )
end
看到下面有一个if语句与方法后面的形参里面的trans不符合。。。原来是if语句后面的参数写错了,我说的呢,把
if transitive
修改为
if trans
OK,问题解决了,哎,可能ruby以后的版本会修复这个bug吧。。。。。。
呃……又发现了一个问题……
提示:private method `gsub' called for 6:Fixnum
看来gsub这个方法还不能支持数字为属性值的标签转换……打开rexml文件夹下面的text.rb,找到代码,大概在289行
def Text::normalize( input, doctype=nil, entity_filter=nil )
copy = input
# Doing it like this rather than in a loop improves the speed
#copy = copy.gsub( EREFERENCE, '&' )
copy = copy.gsub( "&", "&" )
............省略后面的
修改代码
copy=input
为
ruby = input.to_s
解决!
如果还有错误还得解决……rexml的作者可真粗心呀。。。。