简体   繁体   English

使用Nokogiri如何使用Nokogiri :: XML :: Text更改节点的文本值参数数量错误

[英]Using Nokogiri How to Change Text Value of Node using Nokogiri::XML::Text wrong number of arguments

I have a html file that looks like this 我有一个看起来像这样的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>

I have a json file. 我有一个json文件。 I am using the json ruby library to parse it and its coming back as a hash. 我正在使用json ruby library进行解析,并将其作为哈希返回。 I need to use the keys and insert the values into this html file. 我需要使用键并将值插入此html文件中。

so far my ruby script looks like this 到目前为止,我的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

I saw someone's example here -> Insert Text After Specific XML Tag in Nokogiri 我在这里看到了某人的示例-> 在Nokogiri中的特定XML标记后插入文本

I am getting this error 我收到此错误

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

The doc does not have examples, its not working for me. 该文档没有示例,它对我不起作用。

You are using the class Nokogiri::XML::Text , but need to use Nokogiri::XML::Builder as they did in the example. 您正在使用类Nokogiri::XML::Text ,但需要像示例中一样使用Nokogiri::XML::Builder Nokogiri::XML::Text.new takes 2+ arguments http://nokogiri.org/Nokogiri/XML/Text.html Nokogiri::XML::Text.new接受2个以上的参数http://nokogiri.org/Nokogiri/XML/Text.html

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

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