将XML文档中的各个行转换为哈希?

时间:2014-02-27 21:29:54

标签: ruby-on-rails xml xpath nokogiri

我有以下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解析它并将其转换为哈希值,以便我可以为哈希创建一个DB记录,如:

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"]

但这并没有让我能够将这些记录分开。

2 个答案:

答案 0 :(得分:0)

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}

答案 1 :(得分:0)

我对这个问题的回答如何:https://stackoverflow.com/a/10144623/405017

有了它,你可以在任何一个Nokogiri元素上调用to_hash并获得一个很好的哈希值。

然后,您可以(如果需要)只选择所需哈希的字段:Slicing params hash for specific values