如何使用 XmlSimple 将哈希解析为 XML?
我有一个散列,我想使用 SimpleXML 解析为 XML,但有一个我不知道如何处理的技巧:
我的散列看起来像:
require 'xmlsimple'
test = { "subroot" => {
field1 => {'var1' = ['xyz'], 'var2' = ['yyyy']},
field2 => {'var1' = ['xyz'], 'var2' = ['yyyy']},
field3 => {'var1' = ['xyz'], 'var2' = ['yyyy']},
'id' = 'xxxxxx'} }
我使用以下方法将其解析为 XML:
XmlSimple.xml_out(teste, 'RootName' => 'root')
导致:
<cenario>
<subroot id="xxxxxx">
<field1>
<var1>xyz</var1>
<var2>yyyy</var2>
</field1>
<field2>
<var1>xyz</var1>
<var2>yyyy</var2>
</field2>
<field3>
<var1>xyz</var1>
<var2>yyyy</var2>
</field3>
</subroot>
</cenario>
没关系,工作起来就像一个魅力,但有当我需要循环时出现问题。 我正在做一个自动化测试,在每个测试上循环并将测试数据保存在哈希上,在测试结束时我需要将其保存在 xml 上,我需要得到这样的东西:
<cenario>
<subroot id="xxxxxx">
<field1>
<var1>xyz</var1>
<var2>yyyy</var2>
</field1>
<field2>
<var1>xyz</var1>
<var2>yyyy</var2>
</field2>
<field3>
<var1>xyz</var1>
<var2>yyyy</var2>
</field3>
</subroot>
.
.
.
<subroot id="xxxx10x">
<field1>
<var1>xyz</var1>
<var2>yyyy</var2>
</field1>
<field2>
<var1>xyz</var1>
<var2>yyyy</var2>
</field2>
<field3>
<var1>xyz</var1>
<var2>yyyy</var2>
</field3>
</subroot>
</cenario>
合并每个测试上的每个哈希不会做这个技巧 cz 它可以防止最后一个哈希(子根) 我说清楚了吗? simpleXML 不能解决这个问题还是我需要寻找其他解决方案?
I have a hash that i wanted to parse to XML using SimpleXML but there is a trick I dont know how to handle:
My hash looks like:
require 'xmlsimple'
test = { "subroot" => {
field1 => {'var1' = ['xyz'], 'var2' = ['yyyy']},
field2 => {'var1' = ['xyz'], 'var2' = ['yyyy']},
field3 => {'var1' = ['xyz'], 'var2' = ['yyyy']},
'id' = 'xxxxxx'} }
I parse it to XML using:
XmlSimple.xml_out(teste, 'RootName' => 'root')
resulting in:
<cenario>
<subroot id="xxxxxx">
<field1>
<var1>xyz</var1>
<var2>yyyy</var2>
</field1>
<field2>
<var1>xyz</var1>
<var2>yyyy</var2>
</field2>
<field3>
<var1>xyz</var1>
<var2>yyyy</var2>
</field3>
</subroot>
</cenario>
That's OK and works like a charm but there is problem when I need to loop.
Im doing a automated test that loops on each test and save the test data on a Hash and by the end of the tests i need to save it on a xml and i need to get something like this:
<cenario>
<subroot id="xxxxxx">
<field1>
<var1>xyz</var1>
<var2>yyyy</var2>
</field1>
<field2>
<var1>xyz</var1>
<var2>yyyy</var2>
</field2>
<field3>
<var1>xyz</var1>
<var2>yyyy</var2>
</field3>
</subroot>
.
.
.
<subroot id="xxxx10x">
<field1>
<var1>xyz</var1>
<var2>yyyy</var2>
</field1>
<field2>
<var1>xyz</var1>
<var2>yyyy</var2>
</field2>
<field3>
<var1>xyz</var1>
<var2>yyyy</var2>
</field3>
</subroot>
</cenario>
merging each hash on each test will not do the trick cz it prevents the last hash (subroot)
Did i made myself clear?
Can't simpleXML do the trick or do I need to look for another solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
感谢您的帮助...
我发现如果我将哈希的“实例”放入数组中,我不需要哈希键...
我可以使用相同的哈希名称,每个数组槽都保存我的哈希实例,而不是我可以调用 simpleXML 或 to_xml
不,我只需要弄清楚如何删除使 xml 更大的对象标签。
Thks for the help...
I figured out that i dont need Hash keys if i put a "instance" of the hash inside a array...
I can use the same Hash name, each array slot saves a instance of my hash than i can call simpleXML or to_xml
No i just need to figure out how to remove the objects tags that makes the xml bigger.