【发布时间】: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)
该文档没有示例,它对我不起作用。
【问题讨论】: