Skip to content

Instantly share code, notes, and snippets.

@hamakn
Created November 22, 2012 06:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hamakn/4129620 to your computer and use it in GitHub Desktop.
Save hamakn/4129620 to your computer and use it in GitHub Desktop.
suburi of nokogiri
# coding: utf-8
require "nokogiri"
# Nokogiriの素振り
# <hosts>
# <host address="localhost">
# </hosts>
# を
# <hosts>
# <host address="172.16.0.1">
# <host address="172.16.0.2">
# </hosts>
# にしたい
conf = Nokogiri.XML('<hosts><host address="localhost"></hosts>')
hosts = conf.at_css "hosts"
nodes = Nokogiri::XML::NodeSet.new conf, []
addresses = %w(172.16.0.1 172.16.0.2)
addresses.each do |address|
node = Nokogiri::XML::Node.new "host", conf
node["address"] = address
nodes << node
end
hosts.children = nodes
p conf.to_html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment