Sim*_*yer 9 ruby xml xpath nokogiri
我使用Nokogiri和Ruby来解释XML文件的内容.我想得到一个数组(或类似的)所有元素,<where>在我的例子中是直接的孩子.但是,我得到了各种文本节点(例如"\n\t\t\t"),这是我不想要的.有什么方法可以删除或忽略它们吗?
@body = "
<xml>
<request>
<where>
<username compare='e'>Admin</username>
<rank compare='gt'>5</rank>
</where>
</request>
</xml>" #in my code, the XML contains tab-indentation, rather than spaces. It is edited here for display purposes.
@noko = Nokogiri::XML(@body)
xml_request = @noko.xpath("//xml/request")
where = xml_request.xpath("where")
c = where.children
p c
Run Code Online (Sandbox Code Playgroud)
上面的Ruby脚本输出:
[#<Nokogiri::XML::Text:0x100344c "\n\t\t\t">, #<Nokogiri::XML::Element:0x1003350 name="username" attributes=[#<Nokogiri::XML::Attr:0x10032fc name="compare" value="e">] children=[#<Nokogiri::XML::Text:0x1007580 "Admin">]>, #<Nokogiri::XML::Text:0x100734c "\n\t\t\t">, #<Nokogiri::XML::Element:0x100722c name="rank" attributes=[#<Nokogiri::XML::Attr:0x10071d8 name="compare" value="gt">] children=[#<Nokogiri::XML::Text:0x1006cec "5">]>, #<Nokogiri::XML::Text:0x10068a8 "\n\t\t">]
我想以某种方式获得以下对象:
[#<Nokogiri::XML::Element:0x1003350 name="username" attributes=[#<Nokogiri::XML::Attr:0x10032fc name="compare" value="e">] children=[#<Nokogiri::XML::Text:0x1007580 "Admin">]>, #Nokogiri::XML::Element:0x100722c name="rank" attributes=[#<Nokogiri::XML::Attr:0x10071d8 name="compare" value="gt">] children=[#<Nokogiri::XML::Text:0x1006cec "5">]>]
目前我可以解决这个问题
c.each{|child|
if !child.text?
...
end
}
Run Code Online (Sandbox Code Playgroud)
但是c.length == 5.如果有人可以建议如何从c中排除直接子文本节点,那么这将使我的生活更轻松c.length == 2
Phr*_*ogz 14
您有(至少)三个选项可供选择:
用c = where.element_children而不是c = where.children.
直接选择子元素:
c = xml_request.xpath('./where/*')或
c = where.xpath('./*')
将子列表过滤为仅包含元素的列表:
c = where.children.select(&:element?)
| 归档时间: |
|
| 查看次数: |
11176 次 |
| 最近记录: |