【问题标题】:How Can I Make ActiveResource XML Parsing More Consistent?如何使 ActiveResource XML 解析更加一致?
【发布时间】:2023-03-13 07:25:01
【问题描述】:

我正在使用 ActiveResource 来使用 Redmine 提供的 REST 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="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> 

但是,当 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

没错,它会从任何有值的东西中抛出所有属性信息,但保留来自空元素的属性信息。

不用说,当您尝试查找 id 15(或其他)的值时,这会使事情变得相当困难。现在我可以通过它们的位置来引用事物,但这很脆弱,因为这些元素将来可能会发生变化。我认为必须有某种方法让 ActiveResource 保留属性信息,但因为我没有做任何特别的事情。

(我的 ActiveResource 扩展只有五行:它扩展了 ActiveResource,定义了服务的 url、用户名和密码,仅此而已)。

那么,有谁知道我怎样才能让 ActiveResource 不那么奇怪地解析这个 XML?

【问题讨论】:

  • 如果你能发布一段打印和解析的代码会很酷。
  • 我已经删除了它,但它基本上只是问题 = Redmine::Issue.find(:all);问题[0].custom_fields.each 做 |field|放场;结束

标签: ruby-on-rails xml activeresource


【解决方案1】:

这显然是 ActiveResource 的一个已知问题:

https://github.com/rails/rails/issues/588

不幸的是,似乎没有对此采取任何措施,并且该问题已关闭。如果您愿意,更新 ActiveResource 和 Hash.from_xml 以保留所有属性的 Rails 3 代码都在下面的要点中,您可以在 Redmine 模块中创建定制版本来修复它:

https://gist.github.com/971598

更新:

另一种选择,如 ActiveResource will not be part of Rails 4 core 所示,将作为单独的 gem 分离出来,是为 REST API 使用另一种 ORM,例如 Her

Her 允许您为您的 XML 使用自定义解析器。这是一个名为 Redmine::ParseXML 的示例自定义解析器:

https://gist.github.com/3879418

那么你需要做的就是创建一个像 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

你会得到一个像下面这样的哈希:

#<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"}}>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-11
    • 1970-01-01
    • 2011-06-17
    • 2013-01-31
    • 2016-10-30
    • 2017-05-20
    • 2019-09-13
    相关资源
    最近更新 更多