简体   繁体   中英

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>
  <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. I am using the json ruby library to parse it and its coming back as a hash. I need to use the keys and insert the values into this html file.

so far my ruby script looks like this

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

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.new takes 2+ arguments http://nokogiri.org/Nokogiri/XML/Text.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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