繁体   English   中英

如何在Rails中搜索特定的字符串?

[英]how to search a particular string in rails?

我已经像下面这样说了,我想搜索一个特定的字符串,在rails中执行此操作很重要。

例如:我想搜索Nokogiri :: XML :: Text模式并获取所有与Nokogiri :: XML :: Text匹配的模式

#<LinkedIn::Location:0x4a339e8 @doc=#<Nokogiri::XML::Document:0x2519cb8 name="document" children=[#<Nokogiri::XML::Element:0x2519a0c name="per
son" children=[#<Nokogiri::XML::Text:0x25197f0 "\n  ">, #<Nokogiri::XML::Element:0x251970c name="`" children=[#<Nokogiri::XML::Text:0x2
519124 "\n    ">, #<Nokogiri::XML::Element:0x25190f4 name="name" children=[#<Nokogiri::XML::Text:0x2518bd8 "Bengaluru Area, India">]>, #<Nokog
iri::XML::Text:0x2518b00 "\n    ">, #<Nokogiri::XML::Element:0x2518ad0 name="country" children=[#<Nokogiri::XML::Text:0x2518680 "\n      ">, #
<Nokogiri::XML::Element:0x2518650 name="code" children=[#<Nokogiri::XML::Text:0x2517fcc "in">]>, #<Nokogiri::XML::Text:0x2517eb8 "\n    ">]>,
#<Nokogiri::XML::Text:0x2517cd8 "\n  ">]>, #<Nokogiri::XML::Text:0x2517bdc "\n">]>]>>

您可以获得类似的值:

方式一:

reader = Nokogiri::XML::Reader(xml)
reader.read #Moves to next node in document
reader.attribute("cdn") # To get the value of attributes

方式二:

doc = Nokogiri::XML(xml)
elems = doc.xpath("//*[@messageId]") #get all elements with an attribute of 'messageId'
elems[0].attr('messageId') #gets value of attribute of first elem

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM