【发布时间】:2016-10-18 14:45:29
【问题描述】:
我正在尝试学习 RubyMine 中的 XML 处理,基于 nokogiri.com 上的教程以及我在 stackoverflow.com 上的论坛中找到的内容。
我的代码是:
Then /^I process an XML file:(.*?).$/ do |arg1|
xml_str = Nokogiri::XML('<root>
<sitcoms>
<sitcom>
<name>Married with Children</name>
<characters>
<character>Al Bundy</character>
<character>Bud Bundy</character>
<character>Marcy Darcy</character>
</characters>
</sitcom>
<sitcom>
<name>Perfect Strangers</name>
<characters>
<character>Larry Appleton</character>
<character>Balki Bartokomous</character>
</characters>
</sitcom>
</sitcoms>
<dramas>
<drama>
<name>The A-Team</name>
<characters>
<character>John "Hannibal" Smith</character>
<character>Templeton "Face" Peck</character>
<character>"B.A." Baracus</character>
<character>"Howling Mad" Murdock</character>
</characters>
</drama>
</dramas>
</root>
')
doc = Nokogiri::XML(xml_str)
sleep 2
puts "\ndoc class is " + doc.class.to_s
sleep 2
thing = doc.xpath("//character")
sleep 2
puts "\nthing class is " + thing.class.to_s
sleep 2
stop_value = thing.length
idx = 0
puts "\nthing length is #{stop_value}"
if thing.empty?
puts "\nthing is empty"
else
pits "\nthing is NOT empty"
end
while idx < stop_value
puts "\n" + thing[idx].to_s
idx += 1
end
“doc”的类是正确的,但“thing”的类是 Nil。
我正在运行 RubyMine 8.0.3、Ruby 2.2.1 和 Appium 1.5.3。
提前致谢。
【问题讨论】:
标签: ruby xml xml-parsing appium