如何解析 XML 并根据节点内容执行操作?

发布于 2024-12-19 04:00:21 字数 555 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷爱 2024-12-26 04:00:22
f = File.open("file.xml")
doc = Nokogiri::XML(f)
f.close

result = []

doc.root.children.each do |node|
  if node.name == "string"
    result.push(node.inner_text)
    repeat = node.children[0]
    times = repeat["times"]
    for i in 1..times do
      result.append(repeat.inner_text)
    end
  end
  ...
end

" ".join(result)

类似的事情。老实说,我自己没有使用过 Nokogiri,但希望这对您有所帮助。

f = File.open("file.xml")
doc = Nokogiri::XML(f)
f.close

result = []

doc.root.children.each do |node|
  if node.name == "string"
    result.push(node.inner_text)
    repeat = node.children[0]
    times = repeat["times"]
    for i in 1..times do
      result.append(repeat.inner_text)
    end
  end
  ...
end

" ".join(result)

Something like that. To be honest, I haven't used Nokogiri myself, but hopefully, this was helpful.

清风夜微凉 2024-12-26 04:00:21

我的尝试使用递归和映射(我认为函数式编程很优雅:)

需要“nokogiri”

def build_string_from_xml(nodes)
  nodes.map { |node|
    inner_str = build_string_from_xml(node.xpath("./*"))
    case node.name
    when "string"
      node.content
    when "repeat"
      if node[:type] == "numbered"
        1.upto(node[:times].to_i).map { |i|
          inner_str + i.to_s
        }.join
      else
        inner_str * node[:times].to_i
      end
    when "manipulate"
      if node[:type] == "caps"
        inner_str.upcase
      else
        raise ArgumentError, "Don't know that manipulation type: %s" % node[:type]
      end
    else
      raise ArgumentError, "Don't know that tag: %s" % node.name
    end
  }.join
end

doc = Nokogiri::XML.parse(<<-XML)
<mystring>
  <manipulate type="caps">
    <string>Hello There!</string>
    <repeat times="4">
      <string> FooBar</string>
    </repeat>
    <string>!</string>
  </manipulate>

  <repeat times="3" type="numbered">
    <string> FooBar</string>
  </repeat>
</mystring>
XML

p build_string_from_xml(doc.xpath("//mystring/*"))

My attempt, which uses recursion and map (I consider functional programming elegant :)

require 'nokogiri'

def build_string_from_xml(nodes)
  nodes.map { |node|
    inner_str = build_string_from_xml(node.xpath("./*"))
    case node.name
    when "string"
      node.content
    when "repeat"
      if node[:type] == "numbered"
        1.upto(node[:times].to_i).map { |i|
          inner_str + i.to_s
        }.join
      else
        inner_str * node[:times].to_i
      end
    when "manipulate"
      if node[:type] == "caps"
        inner_str.upcase
      else
        raise ArgumentError, "Don't know that manipulation type: %s" % node[:type]
      end
    else
      raise ArgumentError, "Don't know that tag: %s" % node.name
    end
  }.join
end

doc = Nokogiri::XML.parse(<<-XML)
<mystring>
  <manipulate type="caps">
    <string>Hello There!</string>
    <repeat times="4">
      <string> FooBar</string>
    </repeat>
    <string>!</string>
  </manipulate>

  <repeat times="3" type="numbered">
    <string> FooBar</string>
  </repeat>
</mystring>
XML

p build_string_from_xml(doc.xpath("//mystring/*"))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文