InQusitive InQusitive - 1 year ago 82
Ruby Question

Why isn't my Nokogiri working?

This is my first code in ruby using Nokogiri. But it doesn't work and I am not able to understand why.

<?xml version="1.0"?>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<description>An in-depth look at creating applications
with XML.</description>


require 'nokogiri'

f ="test.xml")
doc = Nokogiri::XML(f)
nodes = doc.xpath("title")
nodes.each do |node|
print node.text

Here I am getting the nodes as a nil class.

Answer Source

To select nodes in the document from the current node that match the selection no matter where they are try

nodes = doc.xpath("//title")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download