DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Convert Wordpress Export File To Import Blogger

12.04.2008
| 10584 views |
  • submit to reddit
        // wordpress2blogger.rb

require 'rexml/document'
include REXML # so that we don't have to prefix everything with REXML::...

file = File.new( "E:\\Downloads\\Blogger\\www.caiwangqin.com.xml" )
doc = REXML::Document.new file

content1 = <<EOF
<?xml version="1.0" encoding="UTF-8" ?> 
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0">
  <id>tag:blogger.com,1999:blog-4684235500622854327.archive</id> 
  <updated>2008-12-04T11:08:02.017+08:00</updated> 
  <title type="text">Caiwangqin’s blog</title> 
  <link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://blog.caiwangqin.com/feeds/archive" /> 
  <link rel="self" type="application/atom+xml" href="http://blog.caiwangqin.com/feeds/archive" /> 
  <link rel="http://schemas.google.com/g/2005#post" type="application/atom+xml" href="http://www.blogger.com/feeds/4684235500622713456/archive" /> 
  <link rel="alternate" type="text/html" href="http://blog.caiwangqin.com/" /> 
  <author>
  <name>Caiwangqin</name> 
  <uri>http://www.blogger.com/profile/06233150076014685286</uri> 
  <email>noreply@blogger.com</email> 
  </author>
  <generator version="7.00" uri="http://www.blogger.com">Blogger</generator> 
EOF

entries = []

doc.elements.each("//item") { |element| 
  title = element.get_text("title")
  datestr = "#{element.get_text("pubDate")}"
  d = DateTime.parse(datestr).strftime("%Y-%m-%dT%H:%M:%S+08:00")
  desc = element.get_text("content:encoded")
  category = element.get_text("category")
  category = "Blogging" if category == "" or category.nil?
  
entry = <<EOF
	<entry>
  <id>tag:blogger.com,1999:blog-4684235500622716427.post-728474462564654977</id> 
  <published>#{d}</published>
  <updated>#{d}</updated>
  <category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/blogger/2008/kind#post" /> 
  <category scheme="http://www.blogger.com/atom/ns#" term="#{category}" /> 
  <title type="text">#{title}</title> 
  <content type="html"><![CDATA[#{desc}]]></content> 
  <author>
  <name>Caiwangqin</name> 
  <uri>http://www.blogger.com/profile/06233150076014685286</uri> 
  <email>jesse.cai@gmail.com</email> 
  </author>
  <thr:total>0</thr:total> 
  </entry>
EOF

  entries << entry
}

content2 = <<EOF
</feed>
EOF


n = entries.size.divmod(50)
i = n[1] > 0 ? n[0] + 1 : n[0]

a = 0
i.times do |x|
  content = ""
  50.times do |t|
    break if a > entries.size - 1
    content = content + entries[a]
    a = a + 1
  end
  puts a
  destination = "E:\\Downloads\\Blogger\\wp2blogger#{x}.xml"
  File.open(destination,"w") do |f|
    f.write(content1 + content + content2)
  end    
end