Created
November 22, 2012 06:05
-
-
Save hamakn/4129620 to your computer and use it in GitHub Desktop.
suburi of nokogiri
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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