我有以下XML文档:
<AccountPerformanceReportColumns>
<Column name="AccountName" />
<Column name="Impressions" />
<Column name="Clicks" />
<Column name="Ctr" />
<Column name="Conversions" />
<Column name="CostPerConversion" />
<Column name="Spend" />
</AccountPerformanceReportColumns>
<Table>
<Row>
<AccountName value="Cleveland" />
<Impressions value="5822" />
<Clicks value="138" />
<Ctr value="2.37" />
<Conversions value="0" />
<CostPerConversion value="" />
<Spend value="238.28" />
</Row>
<Row>
<AccountName value="Denver" />
<Impressions value="8196" />
<Clicks value="123" />
<Ctr value="1.50" />
<Conversions value="0" />
<CostPerConversion value="" />
<Spend value="258.32" />
</Row>
<Row>
<AccountName value="Houston" />
<Impressions value="7218" />
<Clicks value="105" />
<Ctr value="1.45" />
<Conversions value="3" />
<CostPerConversion value="75.88" />
<Spend value="227.63" />
</Row>
<Row>
<AccountName value="LA" />
<Impressions value="72290" />
<Clicks value="713" />
<Ctr value="0.99" />
<Conversions value="0" />
<CostPerConversion value="" />
<Spend value="932.93" />
</Row>
<Row>
<AccountName value="Louisville" />
<Impressions value="2811" />
<Clicks value="68" />
<Ctr value="2.42" />
<Conversions value="0" />
<CostPerConversion value="" />
<Spend value="167.09" />
</Row>
</Table>
我正在尝试使用 Nokogiri 解析它并将其转换为哈希,以便我可以为哈希创建数据库记录,例如:
BingRecords.create!(conversions: hash[:conversion],
spend: hash[:spend],
account_name: hash[:account_name],
date: date,
user_id: user.id)
其中像这样:
hash[:account_name] => ["Cleveland", "Denver", "Houston", "LA", "Louisville"]
我尝试做这样的事情:
bingstats = doc.xpath("//AccountName", "//Spend", "//Conversions")
这给了我这些标签中的所有数据,后跟返回的stats = bingstats.map {|map| map.values}
:
[["Cleveland], ["Denver"], ["Houston"], ["LA"], ["Louisville"], ["238.28"], ["258.32"]
但这并不能使我能够将它们分开以用于单独的记录。
我对这个问题的回答怎么样:https://stackoverflow.com/a/10144623/405017
有了它,您只需在任何单个Nokogiri元素上调用to_hash
并返回一个不错的哈希值。
然后,您可以(如果需要)仅选择所需的哈希字段: 对特定值的参数哈希进行切片
doc = Nokogiri::XML(file).remove_namespaces!
row = doc.xpath("//Row")
account_name_tag = row.xpath("//AccountName")
account_name_values = account_name_tag.map {|map| map.values}
account_name_array = account_name_values.flatten
hash = {account_name: account_name_array}
然后我将重复花费、转换等并将它们添加到哈希中:
hash = {account_name: account_name_array, spend: spend_array, conversions: conversions_array}