使用ruby获取xml属性和值

使用ruby获取xml属性和值,ruby,xml,nokogiri,Ruby,Xml,Nokogiri,我正在使用ruby中的nokogiri库解析网页。我得到的XML结果如下: #<Nokogiri::XML::Element:0x1399140 name="p" children=[#<Nokogiri::XML::Text:0x1398264 "\n">, #<Nokogiri::XML::Element:0x1395d50 name="i" attributes=[#<Nokogiri::XML::Attr:0x1395cb4 name="class" va

我正在使用ruby中的nokogiri库解析网页。我得到的XML结果如下:

#<Nokogiri::XML::Element:0x1399140 name="p" children=[#<Nokogiri::XML::Text:0x1398264 "\n">, 
#<Nokogiri::XML::Element:0x1395d50 name="i" attributes=[#<Nokogiri::XML::Attr:0x1395cb4 name="class" value="icon-pctgrm p-01_g">,
#<Nokogiri::XML::Attr:0x1395ca8 name="title" value="new">]>,
#<Nokogiri::XML::Text:0x1394e20 "\n">]> 
#

从这个结果中,我需要得到最终结果作为
class:icon pctgrm p-01\g,title:new

首先将xml另存为
test.xml
在此脚本的同一目录中,然后运行此脚本-

require 'nokogiri'

File.foreach(File.open("test.xml")) do |line|
  if /Attr/.match(line)
    print line.split('name=')[-1].split('"')[1] + ":" + line.split('value=')[1].split('"')[1] + ", "
  end
end

这将给出预期的结果-
class:icon pctgrm p-01\g,title:new,

首先将xml另存为
test.xml
在此脚本的同一目录中,然后运行此脚本-

require 'nokogiri'

File.foreach(File.open("test.xml")) do |line|
  if /Attr/.match(line)
    print line.split('name=')[-1].split('"')[1] + ":" + line.split('value=')[1].split('"')[1] + ", "
  end
end

这将给出预期结果-
类:图标pctgrm p-01\g,标题:新建,

好的,您尝试了什么?好的,您尝试了什么?