InQusitive InQusitive - 1 year ago 61
Ruby Question

Why my Nokogiri selector not working?

This is my first attempt using Nokogiri, but it doesn't work and I don't understand why:

This is the XML:

<?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>

I'm using this code:

require 'nokogiri'

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

I am getting the nodes as a nil class.

Answer Source

To select nodes matching the selector try:

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