如何解析 XML 并根据节点内容执行操作?
我有一个 XML 文件,它将输出一个字符串:
<mystring>
<manipulate type="caps">
<string>Hello There!</string>
<repeat times="4">
<string> FooBar</string>
</repeat>
</manipulate>
<string>!</string>
</mystring>
我想要创建的字符串是:
HELLO THERE! FOOBAR FOOBAR FOOBAR FOOBAR!
我想解释 XML 节点并执行某些操作或输出某些字符串。我想要一种干净的方式来做到这一点。这只是一个简化版本,还会有其他具有更复杂功能的节点,但我需要一些入门帮助。
我尝试使用 Nokogiri 来做到这一点,但有点困难。
I have an XML file which will output a string:
<mystring>
<manipulate type="caps">
<string>Hello There!</string>
<repeat times="4">
<string> FooBar</string>
</repeat>
</manipulate>
<string>!</string>
</mystring>
The string I want this to create is:
HELLO THERE! FOOBAR FOOBAR FOOBAR FOOBAR!
I would like to interpret the XML nodes and perform certain actions or output certain strings. I would like a clean way of doing this. This is just a simplified version and there will be other nodes with more complicated functions, but I need some help getting started.
I attempted to do it using Nokogiri but am struggling a little bit.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
类似的事情。老实说,我自己没有使用过 Nokogiri,但希望这对您有所帮助。
Something like that. To be honest, I haven't used Nokogiri myself, but hopefully, this was helpful.
我的尝试使用递归和映射(我认为函数式编程很优雅:)
需要“nokogiri”
My attempt, which uses recursion and map (I consider functional programming elegant :)
require 'nokogiri'