Skip to content

Instantly share code, notes, and snippets.

@dtan4
Created March 23, 2014 08:42
Show Gist options
  • Save dtan4/9720461 to your computer and use it in GitHub Desktop.
Save dtan4/9720461 to your computer and use it in GitHub Desktop.
Convert <body> to JSON
#!/usr/bin/env ruby
require "nokogiri"
html = <<-EOS
<!DOCTYPE html>
<html>
<head>
<title>hoge</title>
</head>
<body>
<h1>hoge</h1>
<div class="container">
<h2>fuga</h2>
<ul>
<li>foo</li>
<li>bar</li>
</ul>
</div>
</body>
</html>
EOS
def get_children(element)
children = []
element.children.select { |el| el.is_a? Nokogiri::XML::Element }.each do |el|
children << { name: el.name, children: get_children(el) }
end if element.children.length > 0
children
end
body = Nokogiri::HTML.parse(html).css("body")
json = { name: "body", children: get_children(body) }
p json #=> {:name=>"body", :children=>[{:name=>"h1", :children=>[]}, {:name=>"div", :children=>[{:name=>"h2", :children=>[]}, {:name=>"ul", :children=>[{:name=>"li", :children=>[]}, {:name=>"li", :children=>[]}]}]}]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment