Question

Is there a more elegant way to get the contents of text nodes with Nokogiri? Right now I'm using something like this:

<?xml version="1.0" encoding="UTF-8"?>
  <TestCase>
    <ID>53</ID>
    <Code>WKDO-188465</Code>
  </TestCase>


xml   = Nokogiri::XML(File.open('test.xml'))
id    = xml.at_css('TestCase ID').children.first.text
code  = xml.at_css('TestCase Code').children.first.text
Was it helpful?

Solution

In this case you use the text-method on the elements directly.

xml   = Nokogiri::XML(File.open('test.xml'))
id    = xml.at_css('TestCase ID').text
code  = xml.at_css('TestCase Code').text
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top