Ruby on rails 如何使ActiveResource XML解析更加一致?
我正在使用ActiveResource使用Redmine(bug跟踪工具)提供的RESTWebService。该Web服务生成如下所示的XML:Ruby on rails 如何使ActiveResource XML解析更加一致?,ruby-on-rails,xml,activeresource,Ruby On Rails,Xml,Activeresource,我正在使用ActiveResource使用Redmine(bug跟踪工具)提供的RESTWebService。该Web服务生成如下所示的XML: <custom_field name="Issue Owner" id="15">Fred Fake</custom_field> <custom_field name="Needs Printing" id="16">0</custom_field> <custom_field name="R
<custom_field name="Issue Owner" id="15">Fred Fake</custom_field>
<custom_field name="Needs Printing" id="16">0</custom_field>
<custom_field name="Review Assignee" id="17">Fran Fraud</custom_field>
<custom_field name="Released On" id="20"></custom_field>
<custom_field name="Client Facing" id="21">0</custom_field>
<custom_field name="Type" id="22">Bug</custom_field>
<custom_field name="QA Assignee" id="23"></custom_field>
<custom_field name="Company Name" id="26"></custom_field>
<custom_field name="QA Notes" id="27"></custom_field>
<custom_field name="Failed QA Attempts" id="28">2</custom_field>
#<Redmine::Issue(issues) issues={:attributes=>{:type=>"array", :count=>1640},
:issue=>{:id=>4326,
:project=>{:attributes=>{:name=>"Redmine", :id=>1}},
:tracker=>{:attributes=>{:name=>"Feature", :id=>2}},
:status=>{:attributes=>{:name=>"New", :id=>1}},
:priority=>{:attributes=>{:name=>"Normal", :id=>4}},
:author=>{:attributes=>{:name=>"John Smith", :id=>10106}},
:category=>{:attributes=>{:name=>"Email notifications", :id=>9}},
:subject=>"\n Aggregate Multiple Issue Changes for Email Notifications\n ",
:description=>"\n This is not to be confused with another useful proposed feature that\n would do digest emails for notifications.\n ",
:start_date=>"2009-12-03",
:due_date=>{},
:done_ratio=>0,
:estimated_hours=>{},
:custom_fields=>{
:custom_field=>[
{:attributes=>{:name=>"Issue Owner", :id=>15}, "value"=>"Fred Fake"},
{:attributes=>{:name=>"Needs Printing", :id=>16}, "value"=>0},
{:attributes=>{:name=>"Review Assignee", :id=>17}, "value"=>"Fran Fraud"},
{:attributes=>{:name=>"Released On", :id=>20}},
{:attributes=>{:name=>"Client Facing", :id=>21}, "value"=>0},
{:attributes=>{:name=>"Type", :id=>22}, "value"=>"Bug"},
{:attributes=>{:name=>"QA Assignee", :id=>23}},
{:attributes=>{:name=>"Company Name", :id=>26}},
{:attributes=>{:name=>"QA Notes", :id=>27}},
{:attributes=>{:name=>"Failed QA Attempts", :id=>28}, "value"=>2}]},
:created_on=>"Thu Dec 03 15:02:12 +0100 2009",
:updated_on=>"Sun Jan 03 12:08:41 +0100 2010"}}>
Fred假
0
弗兰欺诈
0
缺陷
2.
但是,当ActiveResource解析该结果,并在打印结果时进行迭代时,我得到:
Fred Fake
0
Fran Fraud
#<Redmine::Issue::CustomFields::CustomField:0x5704e95d>
0
Bug
#<Redmine::Issue::CustomFields::CustomField:0x32fd963>
#<Redmine::Issue::CustomFields::CustomField:0x3a68f437>
#<Redmine::Issue::CustomFields::CustomField:0x407964d6>
2
Fred假
0
弗兰欺诈
#
0
缺陷
#
#
#
2.
没错,它从任何具有值的元素中抛出所有属性信息,但保留空元素中的属性信息
不用说,当您试图找到ID15(或其他)的值时,这使得事情变得相当困难。现在我可以根据它们的位置来引用它们,但这是非常脆弱的,因为这些元素在未来可能会发生变化。我想一定有办法让ActiveResource保留属性信息,但因为我没有做任何特殊的事情
(我的ActiveResource扩展只有五行:它扩展了ActiveResource,定义了服务的url、用户名和密码,仅此而已)
那么,有人知道我如何使ActiveResource不那么奇怪地解析这个XML吗?这是ActiveResource的一个已知问题,显然: 不幸的是,似乎对此束手无策&问题已经解决。如果您觉得合适,下面的要点中包含了更新ActiveResource和Hash.from_xml以保留所有属性的Rails 3代码,您可以在Redmine模块中创建一个定制版本来修复它: 更新: 另一种选择,正如它所显示的,并将作为一个单独的gem剥离出来,将是对restapi使用替代ORM,如 Her允许您对XML使用自定义解析器。这是一个名为Redmine::ParseXML的自定义解析器示例: 因此,您只需创建一个类似config/initializers/her.rb的文件:
Her::API.setup :url => "https://api.xxxxx.org" do |connection|
connection.use Faraday::Request::UrlEncoded
connection.use Redmine::ParseXML
connection.use Faraday::Adapter::NetHttp
end
得到如下所示的散列:
<custom_field name="Issue Owner" id="15">Fred Fake</custom_field>
<custom_field name="Needs Printing" id="16">0</custom_field>
<custom_field name="Review Assignee" id="17">Fran Fraud</custom_field>
<custom_field name="Released On" id="20"></custom_field>
<custom_field name="Client Facing" id="21">0</custom_field>
<custom_field name="Type" id="22">Bug</custom_field>
<custom_field name="QA Assignee" id="23"></custom_field>
<custom_field name="Company Name" id="26"></custom_field>
<custom_field name="QA Notes" id="27"></custom_field>
<custom_field name="Failed QA Attempts" id="28">2</custom_field>
#<Redmine::Issue(issues) issues={:attributes=>{:type=>"array", :count=>1640},
:issue=>{:id=>4326,
:project=>{:attributes=>{:name=>"Redmine", :id=>1}},
:tracker=>{:attributes=>{:name=>"Feature", :id=>2}},
:status=>{:attributes=>{:name=>"New", :id=>1}},
:priority=>{:attributes=>{:name=>"Normal", :id=>4}},
:author=>{:attributes=>{:name=>"John Smith", :id=>10106}},
:category=>{:attributes=>{:name=>"Email notifications", :id=>9}},
:subject=>"\n Aggregate Multiple Issue Changes for Email Notifications\n ",
:description=>"\n This is not to be confused with another useful proposed feature that\n would do digest emails for notifications.\n ",
:start_date=>"2009-12-03",
:due_date=>{},
:done_ratio=>0,
:estimated_hours=>{},
:custom_fields=>{
:custom_field=>[
{:attributes=>{:name=>"Issue Owner", :id=>15}, "value"=>"Fred Fake"},
{:attributes=>{:name=>"Needs Printing", :id=>16}, "value"=>0},
{:attributes=>{:name=>"Review Assignee", :id=>17}, "value"=>"Fran Fraud"},
{:attributes=>{:name=>"Released On", :id=>20}},
{:attributes=>{:name=>"Client Facing", :id=>21}, "value"=>0},
{:attributes=>{:name=>"Type", :id=>22}, "value"=>"Bug"},
{:attributes=>{:name=>"QA Assignee", :id=>23}},
{:attributes=>{:name=>"Company Name", :id=>26}},
{:attributes=>{:name=>"QA Notes", :id=>27}},
{:attributes=>{:name=>"Failed QA Attempts", :id=>28}, "value"=>2}]},
:created_on=>"Thu Dec 03 15:02:12 +0100 2009",
:updated_on=>"Sun Jan 03 12:08:41 +0100 2010"}}>
{:type=>“array”,:count=>1640},
:issue=>{:id=>4326,
:project=>{:attributes=>{:name=>“Redmine”,:id=>1},
:tracker=>{:attributes=>{:name=>“Feature”,:id=>2}},
:status=>{:attributes=>{:name=>“New”,:id=>1},
:priority=>{:attributes=>{:name=>“Normal”,:id=>4},
:author=>{:attributes=>{:name=>“John Smith”,:id=>10106},
:category=>{:attributes=>{:name=>“电子邮件通知”,:id=>9},
:subject=>“\n聚合电子邮件通知的多个问题更改\n”,
:description=>“\n这不应与另一个有用的建议功能相混淆,\n该功能将对通知邮件进行摘要。\n”,
:开始日期=>“2009-12-03”,
:到期日=>{},
:完成率=>0,
:估计时数=>{},
:自定义_字段=>{
:自定义_字段=>[
{:attributes=>{:name=>“问题所有者”,:id=>15},“值”=>“Fred Fake”},
{:attributes=>{:name=>“需要打印”,:id=>16},“value”=>0},
{:attributes=>{:name=>“审查受让人”,:id=>17},“值”=>“Fran欺诈”},
{:attributes=>{:name=>“发布日期”,:id=>20},
{:attributes=>{:name=>“面向客户机的”,:id=>21},“值”=>0},
{:attributes=>{:name=>“Type”,:id=>22},“value”=>“Bug”},
{:attributes=>{:name=>“QA受让人”,:id=>23},
{:attributes=>{:name=>“Company name”,:id=>26},
{:attributes=>{:name=>“QA注释”,:id=>27},
{:attributes=>{:name=>“失败的QA尝试”,:id=>28},“value”=>2}]},
:创建时间=>“Thu Dec 03 15:02:12+0100 2009”,
:更新日期=>“Sun Jan 03 12:08:41+0100 2010”}>
如果你能发布一段进行打印和解析的代码,那就太酷了。我已经删除了它,但基本上只是issues=Redmine::Issue.find(:all);问题[0]。自定义|字段。每个do |字段| puts字段;结束