麻烦使用Nokogiri解析XML文件

时间:2022-06-21 00:34:54

I have a code written to parse an XML file in ruby using 'nokogiri' gem. However the logic that i have currently goes and fetches an entire nodeset but what i really want i to do is go fetch the value of an particular attribute in the nodeset. Need some help on how to achieve this using nokogiri???

我有一个代码,用于使用'nokogiri'gem解析ruby中的XML文件。然而,我目前所拥有的逻辑取出整个节点集,但我真正想要做的是获取节点集中特定属性的值。需要一些帮助,如何使用nokogiri实现这一目标???

This is my XML:

这是我的XML:

<page1>
    <testno>3414 & 3413</testno>
    <testdesc>New Design Testing</testdesc>
    <clientname>Meridian Bioscience</clientname>
    <clientaddress1>3471 River Hills Road</clientaddress1>
    <clientaddress2>Cincinnati, OH 45244</clientaddress2>
    <TestRequestedBy>Ginger Powell</TestRequestedBy>
    <DatePrepared>September 3, 2014</DatePrepared>
</page1>

2 个解决方案

#1


0  

***file.xml***

<page1>
    <testno>3414 & 3413</testno>
    <testdesc>New Design Testing</testdesc>
    <clientname>Meridian Bioscience</clientname>
    <clientaddress1>3471 River Hills Road</clientaddress1>
    <clientaddress2>Cincinnati, OH 45244</clientaddress2>
    <TestRequestedBy>Ginger Powell</TestRequestedBy>
    <DatePrepared>September 3, 2014</DatePrepared>
</page1>

require 'nokogiri'

doc = File.open("file.xml") { |f| Nokogiri::XML(f) }

doc = File.open(“file.xml”){| f | Nokogiri :: XML(f)}

doc.xpath("//page1").each do |item|

doc.xpath(“// page1”)。每个do | item |

    p item.at('testno').text
    p item.at('testdesc').text
    p item.at('clientname').text
    p item.at('clientaddress1').text
    p item.at('clientaddress2').text
    p item.at('TestRequestedBy').text
    p item.at('DatePrepared').text

end

#2


0  

First require 'nokogiri' in your class.

首先要求你的班级'nokogiri'。

To access the value of all the child elements. I have stored it in array finally.

访问所有子元素的值。我最后将它存储在数组中。

elements = []
xml_string = "<page1>
    <testno>3414 & 3413</testno>
    <testdesc>New Design Testing</testdesc>
    <clientname>Meridian Bioscience</clientname>
    <clientaddress1>3471 River Hills Road</clientaddress1>
    <clientaddress2>Cincinnati, OH 45244</clientaddress2>
    <TestRequestedBy>Ginger Powell</TestRequestedBy>
    <DatePrepared>September 3, 2014</DatePrepared>
</page1>"

doc = Nokogiri::XML(xml_string)
parent_node = doc.xpath("//page1")

parent_node.each do |child_element|
 elements << child_element.text.gsub("\n","")
end

p elements

I hope this works for you.

我希望这适合你。

#1


0  

***file.xml***

<page1>
    <testno>3414 & 3413</testno>
    <testdesc>New Design Testing</testdesc>
    <clientname>Meridian Bioscience</clientname>
    <clientaddress1>3471 River Hills Road</clientaddress1>
    <clientaddress2>Cincinnati, OH 45244</clientaddress2>
    <TestRequestedBy>Ginger Powell</TestRequestedBy>
    <DatePrepared>September 3, 2014</DatePrepared>
</page1>

require 'nokogiri'

doc = File.open("file.xml") { |f| Nokogiri::XML(f) }

doc = File.open(“file.xml”){| f | Nokogiri :: XML(f)}

doc.xpath("//page1").each do |item|

doc.xpath(“// page1”)。每个do | item |

    p item.at('testno').text
    p item.at('testdesc').text
    p item.at('clientname').text
    p item.at('clientaddress1').text
    p item.at('clientaddress2').text
    p item.at('TestRequestedBy').text
    p item.at('DatePrepared').text

end

#2


0  

First require 'nokogiri' in your class.

首先要求你的班级'nokogiri'。

To access the value of all the child elements. I have stored it in array finally.

访问所有子元素的值。我最后将它存储在数组中。

elements = []
xml_string = "<page1>
    <testno>3414 & 3413</testno>
    <testdesc>New Design Testing</testdesc>
    <clientname>Meridian Bioscience</clientname>
    <clientaddress1>3471 River Hills Road</clientaddress1>
    <clientaddress2>Cincinnati, OH 45244</clientaddress2>
    <TestRequestedBy>Ginger Powell</TestRequestedBy>
    <DatePrepared>September 3, 2014</DatePrepared>
</page1>"

doc = Nokogiri::XML(xml_string)
parent_node = doc.xpath("//page1")

parent_node.each do |child_element|
 elements << child_element.text.gsub("\n","")
end

p elements

I hope this works for you.

我希望这适合你。