如何从 Nokogiri::XML::Builder 创建一个 Nokogiri::XML::Node [英] How to create a Nokogiri::XML::Node from Nokogiri::XML::Builder

查看:70
本文介绍了如何从 Nokogiri::XML::Builder 创建一个 Nokogiri::XML::Node的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用我正在创建的新 HTML 替换文档中的节点.

I need to replace a node in a document with new HTML I'm creating.

我要替换的节点的类是:

The class of the node I have to replace is:

Nokogiri::XML::Node

我使用 Nokogiri Builder 创建我的片段:

I create my fragment using the Nokogiri Builder:

new_node = Nokogiri::XML::Builder.new do |xml|
  xml.table('border' => '1', 'cellpadding' => '1', 'cellspacing' => '1') {
    xml.thead {
      xml.tr {
        battery_test[0..4].each do |head|
          xml.th_ head["inputValue"]
        end
      }
    }
    xml.tbody {
      battery_test.drop(5).each_slice(5) do |row|
        xml.tr {
          row.each do |item|
            xml.td_ item["inputValue"]
          end
        }
      end
    }
  }
end

但是new_node的类是Nokogiri::XML::Builder.

如何用我用构建器创建的片段替换我的 Nokogiri::XML::Node?

How can I replace my Nokogiri::XML::Node with the fragment I create with the builder?

推荐答案

您不必使用 Builder 创建节点.Nokogiri 允许通过多种方式定义它们.您的问题没有得到很好的提问,因为它缺少必要的信息,但这会让您开始:

You don't have to use Builder to create nodes. Nokogiri allows several ways of defining them. Your question isn't asked well as it's missing essential information, but this will get you started:

require 'nokogiri'

doc = Nokogiri::HTML(<<EOT)
<html>
  <head></head>
  <body>
  </body>
</html>
EOT

puts doc.to_html

# >> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
# >> <html>
# >>   <head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head>
# >>   <body>
# >>   </body>
# >> </html>

我可以使用包含 HTML 的字符串添加表格:

I can add a table using a string containing the HTML:

body = doc.at('body')
body.inner_html = "<table><tbody><tr><td>foo</td><td>bar</td></tr></tbody></table>"

puts doc.to_html

# >> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
# >> <html>
# >>   <head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head>
# >>   <body><table><tbody><tr>
# >> <td>foo</td>
# >> <td>bar</td>
# >> </tr></tbody></table></body>
# >> </html>

修改字符串生成以包含您需要的 HTML,让 Nokogiri 完成繁重的工作,您就完成了.更易于阅读和维护.

Modify the string generation to contain the HTML you need, let Nokogiri do the heavy lifting, and you're done. It's easier to read and maintain.

inner_html= 定义为:

inner_html=(node_or_tags)

node_or_tags 表示您可以传递使用 Builder 创建的节点、从 DOM 中的其他位置截取的节点或包含标记的字符串.

node_or_tags means you can pass a node created using Builder, snipped from some other place in the DOM, or a string containing the markup.

同样:

table = Nokogiri::XML::Node.new('table', doc)
table.class # => Nokogiri::XML::Element

table.add_child('<tbody><tr><td>foo</td><td>bar</td></tr></tbody>')
body = doc.at('body')
body.inner_html = table

puts doc.to_html

# >> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
# >> <html>
# >>   <head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head>
# >>   <body><table><tbody><tr>
# >> <td>foo</td>
# >> <td>bar</td>
# >> </tr></tbody></table></body>
# >> </html>

请注意,table 是一个 Nokogiri::XML::Element.HTML 节点是 XML 节点的子类,所以不要让这让您感到困惑.

Note that table is a Nokogiri::XML::Element. HTML nodes are a subclass of XML nodes so don't let that confuse you.

教程是尝试使用 Nokogiri 进行任何操作的良好起点.在这种情况下,修改 HTML/XML 文档"很有用.此外,备忘单"充满了美好.最后,问题标记为 [nokogiri]"揭示了 Stack Overflow 上的所有热门问题.

The tutorials are good starting points for trying anything with Nokogiri. In this case "Modifying an HTML / XML Document" is useful. Also the "Cheat sheet" is chock-full of goodness. Finally, "Questions tagged [nokogiri]" reveals all the top questions on Stack Overflow.

这篇关于如何从 Nokogiri::XML::Builder 创建一个 Nokogiri::XML::Node的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆