【发布时间】:2014-12-13 00:42:41
【问题描述】:
我正在尝试获取 Response 对象的键和值列表,以便将它们转换为哈希,但我在理解 Nokogiri 时遇到问题。 XML:
<?xml version="1.0" encoding="UTF-8"?>
<xml>
<Response>
<Name>Anonymous</Name>
<ExternalDataReference></ExternalDataReference>
<EmailAddress>hi guys</EmailAddress>
<IPAddress>blahblah</IPAddress>
<Status>0</Status>
..... (approximately 30 more elements within each response tag)
</Response>
(approximately 75 more response tags in the document)
我的目标是为每个响应获得类似的东西:
Name: Anonymous
ExternalDataReference:
EmailAddress: hi guys
IPAddress: blahblah
到目前为止我的代码:
f=File.open("./stufftoparse.xml")
doc = Nokogiri::XML(f)
puts "#{doc.xpath("//Response").keys} \n#{doc.xpath("//Response").values}"
我知道上面的代码不起作用,但我不太明白如何获取 Response 标记中的元素(我不认为它们是 Response 的属性,因为它们在自己的 XML 中)。有人可以解释如何做到这一点吗?请注意,我花了一些时间阅读 Nokogiri 文档,但找不到与 XPATH 示例相关的内容。
其他问题: 如何将响应分开,以便我有这样的东西?
Response1:
Name: Anonymous
ExternalDataReference:
EmailAddress: hi guys
IPAddress: blahblah
Response2:
Name: Anonymous
ExternalDataReference:
EmailAddress: hi guys
IPAddress: blahblah
【问题讨论】: