Skip to content

Instantly share code, notes, and snippets.

@charly
Created August 19, 2009 18:55
Show Gist options
  • Save charly/170578 to your computer and use it in GitHub Desktop.
Save charly/170578 to your computer and use it in GitHub Desktop.
require "rubygems"
require "xml/libxml"
#require "active_support"
def traverse(node)
children = node.children.select {|a| a.element?}
txt = "<h1>HTML output</h1><ul>"
last_depth, depth = 1, 1
while child = children.shift
depth = child.path.split("/").size.to_i - 2
if depth > last_depth
txt << "\n<ul>"
elsif depth < last_depth
txt << "</ul>\n" * (last_depth - depth)
end
txt << "<li>#{child.name} : <i>#{child.content if child.children.all? {|a| a.text?}}</i></li>\n"
a = child.children.select {|c| c.element?}
unless a.empty?
children.unshift(a).flatten!
end
last_depth = depth
end
open("output.html", "w") do |f|
f.write txt << "</ul>"
end
end
xml = XML::Document.file("chaplin.xml")
# p xml.root.namespaces
traverse(xml.root)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment