Ruby - XML to Hash然后Hash to XML,两个XML都不相同

时间:2017-12-08 10:06:19

标签: ruby-on-rails ruby xml hash

我有一个XML:

<response xmlns:msg="http://schemas.xyzlabs.net/2010/messaging/" xmlns:ld="http://schemas.xyzlabs.net/2010/lab-data/">
  <msg:trb proRefId="12345" tesComNa="Xyz Laboratories,Inc.">
    <sample ttz="020206b01" id="1898-57" name="Adam, Franklin" />
  </msg:trb>
</response>

我应该在ruby中读取XML并在UI中(在textarea内部)以可读的HASH格式显示它,用户可以在其中编辑HASH中的值。

然后我应该从哈希中重新构造XML。

但是,初始和最终的XML不同

代码

XML&gt; HASH

h = Hash.from_xml('
  <response xmlns:msg="http://schemas.xyzlabs.net/2010/messaging/" xmlns:ld="http://schemas.xyzlabs.net/2010/lab-data/">
    <msg:trb proRefId="12345" tesComNa="Xyz Laboratories,Inc.">
      <sample ttz="020206b01" id="1898-57" name="Adam, Franklin" />
    </msg:trb>
  </response>
')

输出

#{
#  "response" => {
#    "xmlns:msg"=>"http://schemas.xyzlabs.net/2010/messaging/",
#    "xmlns:ld"=>"http://schemas.xyzlabs.net/2010/lab-data/",
#    "trb" => {
#      "proRefId" => "12345",
#      "tesComNa" => "Xyz Laboratories,Inc.",
#      "sample" => {
#        "ttz" => "020206b01",
#        "id" => "1898-57",
#        "name" => "Adam, Franklin"
#      }
#    }
#  }
#}

假设用户修改了上述内容,我正在尝试将修改后的HASH再次转换为XML:

HASH&gt; XML

h["response"].to_xml(root: "response")

输出

<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<response>
  <xmlns:msg>
    http://schemas.xyzlabs.net/2010/messaging/
  </xmlns:msg>
  <xmlns:ld>
    http://schemas.xyzlabs.net/2010/lab-data/undefined
  </undefinedxmlns:ld>
    undefined
  <trb>
    <proRefId>12345</proRefId>    
    <tesComNa>Xyz Laboratories,Inc.</tesComNa>    
    <sample>      
      <ttz>020206b01</ttz>      
      <id>1898-57</id>      
      <name>Adam, Franklin</name>    
  </sample>
    undefined
    </trb>
  undefined
</response>

我看到很多undefined,任何节点的属性都转换为节点本身。

如果只修改了相应的值(由用户完成),我怎样才能恢复原始XML?

1 个答案:

答案 0 :(得分:0)

我认为这可能与您的环境有关。我已经使用Rails控制台(Rails 5.1.4)测试了您的代码,它按预期工作:

[1] pry(main)> h = Hash.from_xml('
[1] pry(main)*   <response xmlns:msg="http://schemas.xyzlabs.net/2010/messaging/" xmlns:ld="http://schemas.xyzlabs.net/2010/lab-data/">
[1] pry(main)*     <msg:trb proRefId="12345" tesComNa="Xyz Laboratories,Inc.">
[1] pry(main)*       <sample ttz="020206b01" id="1898-57" name="Adam, Franklin" />
[1] pry(main)*     </msg:trb>
[1] pry(main)*   </response>
[1] pry(main)* ')
=> {"response"=>
  {"xmlns:msg"=>"http://schemas.xyzlabs.net/2010/messaging/",
   "xmlns:ld"=>"http://schemas.xyzlabs.net/2010/lab-data/",
   "trb"=>
    {"proRefId"=>"12345",
     "tesComNa"=>"Xyz Laboratories,Inc.",
     "sample"=>{"ttz"=>"020206b01", "id"=>"1898-57", "name"=>"Adam, Franklin"}}}}

[2] pry(main)> h['response']['trb']['tesComNa'] = 'StackOverflow'
=> "StackOverflow"

[3] pry(main)> puts h["response"].to_xml(root: "response")
<?xml version="1.0" encoding="UTF-8"?>
<response>
  <xmlns:msg>http://schemas.xyzlabs.net/2010/messaging/</xmlns:msg>
  <xmlns:ld>http://schemas.xyzlabs.net/2010/lab-data/</xmlns:ld>
  <trb>
    <proRefId>12345</proRefId>
    <tesComNa>StackOverflow</tesComNa>
    <sample>
      <ttz>020206b01</ttz>
      <id>1898-57</id>
      <name>Adam, Franklin</name>
    </sample>
  </trb>
</response>