我正在使用 ActiveResource 来使用 Redmine(一个错误跟踪工具)提供的 REST Web 服务。该 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?