【问题标题】:Processing XML in RubyMine在 RubyMine 中处理 XML
【发布时间】: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


    【解决方案1】:

    问题是您调用了两次Nokogiri::XML:一次在第 2 行,将结果分配给xml_str,然后再次调用doc = Nokogiri::XML(xml_str)。如果您将代码更改为只调用一次,它可以正常工作:

    doc = 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>
    ')
    
    puts "doc class is #{doc.class}"
    # => doc class is Nokogiri::XML::Document
    
    thing = doc.xpath("//character")
    puts "thing class is #{characters.class}"
    # => thing class is Nokogiri::XML::NodeSet
    

    【讨论】:

    • 谢谢,乔丹!一旦我对 nokogiri 进行了第二次调用,我的代码就工作了!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-18
    • 2011-09-14
    • 2021-01-23
    • 2014-05-23
    • 1970-01-01
    相关资源
    最近更新 更多