2016-10-18 2 views
0

Ich versuche XML-Verarbeitung in RubyMine zu lernen, basierend auf einem Tutorial auf nokogiri.com und was ich gefunden habe, die Foren auf stackoverflow.com suchend.XML in RubyMine verarbeiten

Mein Code ist:

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 

Die Klasse für "doc" richtig ist, aber die Klasse für "Ding" ist gleich Null.

Ich laufe RubyMine 8.0.3, Ruby 2.2.1 und Appium 1.5.3.

Vielen Dank im Voraus.

Antwort

1

Das Problem ist, dass Sie Nokogiri::XML zweimal anrufen: Einmal in Zeile 2, wo Sie das Ergebnis xml_str zuweisen, und dann wieder, wenn Sie doc = Nokogiri::XML(xml_str) tun. Wenn Sie Ihren Code ändern, um nur einmal anzurufen, funktioniert es gut:

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 
+0

Danke, Jordan! Als ich den zweiten Anruf bei Nokogiri rausholte, funktionierte mein Code !! –

+0

Gut zu helfen! Wenn meine Antwort Ihr Problem gelöst hat, markieren Sie es bitte als akzeptiert. –