Skip to content

Instantly share code, notes, and snippets.

@flavorjones
Forked from sprsquish/gist:113199
Created May 17, 2009 22:53
Show Gist options
  • Save flavorjones/113210 to your computer and use it in GitHub Desktop.
Save flavorjones/113210 to your computer and use it in GitHub Desktop.
require 'nokogiri'
#
# must set the document root node. one line change to original:
#
n = Nokogiri::XML::Node.new('root', Nokogiri::XML::Document.new)
n.document.root = n
n << Nokogiri::XML::Node.new('child', n.document)
n << (c = Nokogiri::XML::Node.new('child', n.document))
c.default_namespace = 'foo:bar'
#
# but it's clearer like this:
#
doc = Nokogiri::XML::Document.new
doc.root = (n = Nokogiri::XML::Node.new('root', doc))
n << Nokogiri::XML::Node.new('child', doc)
n << (c = Nokogiri::XML::Node.new('child', doc))
c.default_namespace = 'http://foo.com/'
puts doc
puts n.search('child', {'xmlns' => 'http://foo.com/'})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment