【问题标题】:Using Nokogiri How to Change Text Value of Node using Nokogiri::XML::Text wrong number of arguments使用 Nokogiri 如何使用 Nokogiri::XML::Text 更改节点的文本值错误的参数数量
【发布时间】:2014-11-24 11:59:10
【问题描述】:

我有一个看起来像这样的 html 文件

<html>
  <head>
    <title><* page.title *></title>
  </head>
  <body>
    <h1>h<* recipe.name *></h1>
    <* EACH recipes recipe *>
      <* food.name *>
      <* EACH recipe.nicknames nickname *>
        <p><* things to be replaced *></p>
      <* ENDEACH *>
    <* ENDEACH *>
  </body>
</html>

我有一个 json 文件。我正在使用json ruby library 对其进行解析,并将其作为哈希返回。我需要使用键并将值插入到这个 html 文件中。

到目前为止,我的 ruby​​ 脚本看起来像这样

require 'rubygems'
require 'nokogiri'  
require 'json' 

data = File.read("data.json")
obj = JSON.parse(data)

puts obj.values

page = Nokogiri::HTML(open("somethingtemplate"))

# base = Nokogiri::XML::Node.new
# base["href"] = "http://google.com"

# page.xpath('//body/h1').each do |node|
#   node.add_child(base)
#   puts child.text
# end

 builder = Nokogiri::XML::Text.new do page
   page.body {
    page.h1
    page.text "hello world"
   }
 end 

 puts builder.doc

我在这里看到了某人的例子 -> Insert Text After Specific XML Tag in Nokogiri

我收到了这个错误

 `new': wrong number of arguments (0 for 2+) (ArgumentError)

该文档没有示例,它对我不起作用。

【问题讨论】:

    标签: ruby xml json nokogiri


    【解决方案1】:

    您正在使用类Nokogiri::XML::Text,但需要像示例中那样使用Nokogiri::XML::BuilderNokogiri::XML::Text.new 接受 2+ 个参数 http://nokogiri.org/Nokogiri/XML/Text.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-14
      • 1970-01-01
      • 1970-01-01
      • 2012-01-24
      • 1970-01-01
      • 2011-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多