Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
cats_hierarchy={}
(doc/"wp:category").each do |category|
cat_name = category.at("wp:category_nicename").innerHTML
cat_parent = category.at("wp:category_parent").innerHTML
if cats_hierarchy.include? cat_parent
cats_hierarchy[cat_parent] = cat_name
else
cats_hierarchy[cat_name] = nil
end
end
cats = cats_hierarchy.to_a.flatten.uniq
?> cats = cats_hierarchy.to_a.flatten.uniq
=> ["route66", nil, "rails", "american-2008", "reciprocal-affection", "hope-for-the-future", "code", "blog", "review-blog", "rant", "brands", "projects", "yab_shop", "textpattern", "meaningful-labor", "giants", "accessibility", "root", "charity-project", "apple", "xhtml", "america-2006-route-66", "ruby", "learning", "america-2007", "uncategorized", "iphone", "america-2008"]
?> cats_hierarchy
=> {"route66"=>nil, "rails"=>nil, "american-2008"=>nil, "reciprocal-affection"=>nil, "hope-for-the-future"=>nil, "code"=>nil, "blog"=>"review-blog", "rant"=>nil, "brands"=>nil, "projects"=>nil, "yab_shop"=>nil, "textpattern"=>nil, "meaningful-labor"=>nil, "giants"=>nil, "accessibility"=>nil, "root"=>nil, "charity-project"=>nil, "apple"=>nil, "xhtml"=>nil, "america-2006-route-66"=>nil, "ruby"=>nil, "learning"=>nil, "america-2007"=>nil, "uncategorized"=>nil, "iphone"=>nil, "america-2008"=>nil}
<!-- generator="WordPress/2.9.2" created="2010-06-05 19:53"-->
<rss version="2.0"
xmlns:excerpt="http://wordpress.org/export/1.0/excerpt/"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:wp="http://wordpress.org/export/1.0/"
>
<channel>
<title>David Lowry</title>
<link>http://davidlowry.co.uk</link>
<description>Setting out freelance with Web and AV Solutions</description>
<pubDate>Mon, 26 Apr 2010 12:12:31 +0000</pubDate>
<generator>http://wordpress.org/?v=2.9.2</generator>
<language>en</language>
<wp:wxr_version>1.0</wp:wxr_version>
<wp:base_site_url>http://davidlowry.co.uk</wp:base_site_url>
<wp:base_blog_url>http://davidlowry.co.uk</wp:base_blog_url>
<wp:category><wp:category_nicename>accessibility</wp:category_nicename><wp:category_parent></wp:category_parent><wp:cat_name><![CDATA[accessibility]]></wp:cat_name></wp:category>
<wp:category><wp:category_nicename>america-2006-route-66</wp:category_nicename><wp:category_parent></wp:category_parent><wp:cat_name><![CDATA[America 2006 Route 66]]></wp:cat_name></wp:category>
<wp:category><wp:category_nicename>america-2007</wp:category_nicename><wp:category_parent></wp:category_parent><wp:cat_name><![CDATA[America 2007]]></wp:cat_name></wp:category>
<wp:category><wp:category_nicename>america-2008</wp:category_nicename><wp:category_parent></wp:category_parent><wp:cat_name><![CDATA[America 2008]]></wp:cat_name></wp:category>
....
A set of gists from my article on importing Wordpress' exported XML into a ruby application.
@davidlowry

This comment has been minimized.

Copy link
Owner Author

@davidlowry davidlowry commented Jun 5, 2010

cats_hierarchy={}
(doc/"wp:category").each do |category|
cat_name = category.at("wp:category_nicename").innerHTML
cat_parent = category.at("wp:category_parent").innerHTML

if cats_hierarchy.include? cat_parent
    cats_hierarchy[cat_parent] = cat_name
else
    cats_hierarchy[cat_name] = nil
end

end

cats = cats_hierarchy.to_a.flatten.uniq

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment