简体   繁体   English

如何从 Nokogiri::XML::Builder 创建一个 Nokogiri::XML::Node

[英]How to create a Nokogiri::XML::Node from Nokogiri::XML::Builder

I need to replace a node in a document with new HTML I'm creating.我需要用我正在创建的新 HTML 替换文档中的节点。

The class of the node I have to replace is:我必须替换的节点的类是:

Nokogiri::XML::Node

I create my fragment using the Nokogiri Builder:我使用 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

But the class of new_node is Nokogiri::XML::Builder .但是new_node的类是Nokogiri::XML::Builder

How can I replace my Nokogiri::XML::Node with the fragment I create with the builder?如何用我用构建器创建的片段替换我的 Nokogiri::XML::Node?

You don't have to use Builder to create nodes.您不必使用 Builder 来创建节点。 Nokogiri allows several ways of defining them. Nokogiri 允许通过多种方式定义它们。 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>

I can add a table using a string containing the HTML:我可以使用包含 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>

Modify the string generation to contain the HTML you need, let Nokogiri do the heavy lifting, and you're done.修改字符串生成以包含您需要的 HTML,让 Nokogiri 完成繁重的工作,您就完成了。 It's easier to read and maintain.它更易于阅读和维护。

inner_html= is defined as: inner_html=定义为:

inner_html=(node_or_tags)

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. node_or_tags意味着您可以传递使用 Builder 创建的节点、从 DOM 中的其他位置node_or_tags的节点或包含标记的字符串。

Similarly:相似地:

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>

Note that table is a Nokogiri::XML::Element .请注意, tableNokogiri::XML::Element HTML nodes are a subclass of XML nodes so don't let that confuse you. HTML 节点是 XML 节点的子类,所以不要让这让您感到困惑。

The tutorials are good starting points for trying anything with Nokogiri.这些教程是尝试使用 Nokogiri 进行任何操作的良好起点。 In this case " Modifying an HTML / XML Document " is useful.在这种情况下,“ 修改 HTML/XML 文档”很有用。 Also the " Cheat sheet " is chock-full of goodness. 备忘单”也充满了善意。 Finally, " Questions tagged [nokogiri] " reveals all the top questions on Stack Overflow.最后,“ 问题标记 [nokogiri] ”揭示了 Stack Overflow 上的所有热门问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM