如何使用nokogiri查看节点的内容?
How to view the content of a node using nokogiri?
require 'nokogiri'
f = File.open("test.xml")
doc = Nokogiri::XML.parse(f)
description = doc.css('description')
description.each do |item|
print "#{item}\n"
end
输出:
<description>A deep sea diver finds true love twenty
thousand leagues beneath the sea.</description>
<description>An anthology of horror stories about roaches,
centipedes, scorpions and other insects.</description>
<description>After an inadvertant trip through a Heisenberg
Uncertainty Device, James Salway discovers the problems
of being quantum.</description>
<description>Microsoft's .NET initiative is explored in
detail in this deep programmer's reference.</description>
<description>The Microsoft MSXML3 parser is covered in
detail, with attention to XML DOM interfaces, XSLT processing,
SAX and more.</description>
<description>Microsoft Visual Studio 7 is explored in depth,
looking at how Visual Basic, Visual C++, C#, and ASP+ are
integrated into a comprehensive development
environment.</description>
如何防止 nokogiri gem 打印 <description>
标签对?
使用.content
方法如下:
require 'nokogiri'
f = File.open("test.xml")
doc = Nokogiri::XML.parse(f)
description = doc.css('description')
description.each do |item|
print "#{item.content}\n"
end
require 'nokogiri'
f = File.open("test.xml")
doc = Nokogiri::XML.parse(f)
description = doc.css('description')
description.each do |item|
print "#{item}\n"
end
输出:
<description>A deep sea diver finds true love twenty
thousand leagues beneath the sea.</description>
<description>An anthology of horror stories about roaches,
centipedes, scorpions and other insects.</description>
<description>After an inadvertant trip through a Heisenberg
Uncertainty Device, James Salway discovers the problems
of being quantum.</description>
<description>Microsoft's .NET initiative is explored in
detail in this deep programmer's reference.</description>
<description>The Microsoft MSXML3 parser is covered in
detail, with attention to XML DOM interfaces, XSLT processing,
SAX and more.</description>
<description>Microsoft Visual Studio 7 is explored in depth,
looking at how Visual Basic, Visual C++, C#, and ASP+ are
integrated into a comprehensive development
environment.</description>
如何防止 nokogiri gem 打印 <description>
标签对?
使用.content
方法如下:
require 'nokogiri'
f = File.open("test.xml")
doc = Nokogiri::XML.parse(f)
description = doc.css('description')
description.each do |item|
print "#{item.content}\n"
end