博客園搬家到wordpress

歡迎來新博客地址參觀~ https://www.simonjia.top/
在網上搜了一下關於博客園搬家到wordpress的,很多帖子說到《cnblogs2wp》這個插件,但是不知道因爲版本更新的原因還是我的環境問題,一直導入失敗。看了下博客園的備份文件(每天晚上18點以後才能備份,週末24小時)就是xml格式,於是打算用java進行dom4j解析,轉存sql文件,導入到wp_posts表即可。
UxGNCt.png
這裏主要用到dom4j進行xml解析,拼接sql然後以io流的格式存到sql文件中。
主要注意,文章內容要先進行json格式化之後再存入sql文件,不然顯示會出現代碼行錯亂。
UxGR2V.png
以前的文章樣式基本沒變化!奧利給~
下面貼上代碼:
pom文件

<dependency>
			<groupId>dom4j</groupId>
			<artifactId>dom4j</artifactId>
			<version>1.6.1</version>
		</dependency>
		<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.58</version>
        </dependency>



import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 *json工具類
 */
public class JsonUtils {
	private static final ObjectMapper JSON = new ObjectMapper();
	private static final ObjectMapper JSON_NOFORMAT = new ObjectMapper();
	
	static {
		//Include.NON_NULL 屬性爲NULL 不序列化
		JSON.setSerializationInclusion(JsonInclude.Include.NON_NULL);
		//是否縮放排列輸出
		JSON.configure(SerializationFeature.INDENT_OUTPUT, Boolean.TRUE);
		//不解析未知的字符串
		JSON.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		
		//Include.NON_NULL 屬性爲NULL 不序列化
		JSON_NOFORMAT.setSerializationInclusion(JsonInclude.Include.NON_NULL);
		//是否縮放排列輸出
		JSON_NOFORMAT.configure(SerializationFeature.INDENT_OUTPUT, Boolean.FALSE);
		
		JSON_NOFORMAT.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
	}
	
	public static <T> String toJsonNf(T t) {
		try {
			return JSON_NOFORMAT.writeValueAsString(t);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static <T> String toJson(T t) {
		try {
			return JSON.writeValueAsString(t);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static <T> T toObject(String str, Class<T> clazz) {
		try {
			return (T) JSON.readValue(str, clazz);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static <T> T cast2(Class<T> clazz, Map map) {
		return (T) map;
	}
	
	
	public static void main(String args[]) {
		
	}
	
	
}

xml文件解析和創建sql文件



import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class ReadXml {
	
	
	public static void main(String[] args) throws ParseException {
		//創建DOM4J解析器對象
		SAXReader saxReader = new SAXReader();
		int i = 0;
		List<Map> list = new ArrayList<>();
		try {
			//讀取xml文件,並生成document對象 現可通過document來操作文檔
			Document document = saxReader.read("C:\\Users\\Administrator\\Desktop\\CNBlogs_BlogBackup_1_201706_202007.xml");
			//獲取到文檔的根節點
			Element rootElement = document.getRootElement();
			System.out.println("根節點的名字是:" + rootElement.getName());
			//獲取子節點列表
			Iterator it = rootElement.elementIterator();
			while (it.hasNext()) {
				Element fistChild = (Element) it.next();
				//獲取節點的屬性值
				System.out.println("第1個子節點--" + fistChild.getName());
				//獲取子節點的下一級節點
				Iterator iterator = fistChild.elementIterator();
				while (iterator.hasNext()) {
					Element element = (Element) iterator.next();
					System.out.println("第2個子節點--" + element.getName());
					if (element.getName().equalsIgnoreCase("item")) {
						Map map = new HashMap();
						i++;
						Iterator itemElements = element.elementIterator();
						while (itemElements.hasNext()) {
							Element ielement = (Element) itemElements.next();
							String elemetName = ielement.getName();
							String elemetValue = ielement.getStringValue();
							if ("pubDate".equals(elemetName)) {
								elemetValue = getTimestampTimeV17(elemetValue);
							}
							System.out.println("-------item----name---" + elemetName);
//							System.out.println("-------item-----value--" + elemetValue);
							map.put(elemetName, elemetValue);
						}
						list.add(map);
					}
				}
			}
			System.out.println("-----總共有多少文章---------" + i);
			System.out.println("-----總共有多少文章---------" + list.size());
			writeFile(list);
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 格式化GMT時間到北京時間
	 *
	 * @param str
	 * @return
	 * @throws ParseException
	 */
	public static String getTimestampTimeV17(String str) throws ParseException {
		SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss 'GMT'", Locale.US);
		Date date = dateFormat.parse(str);
		//加8個時區
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR, calendar.get(Calendar.HOUR) + 8);
		date = calendar.getTime();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String formatTime = sdf.format(date);
		System.out.println(formatTime);
		return formatTime;
	}
	
	/**
	 * 寫入sql文件
	 *
	 * @param mapList
	 * @throws IOException
	 */
	public static void writeFile(List<Map> mapList) throws IOException {
		//寫入中文字符時解決中文亂碼問題
		FileOutputStream fos = new FileOutputStream(new File("C:\\Users\\Administrator\\Desktop\\wp.sql"));
		OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
		BufferedWriter bw = new BufferedWriter(osw);
		int y = 180;
		for (int i = 0; i < mapList.size(); i++) {
			//所有內容進行json格式化,防止"\n"在sql文件中格式化
			//獲取標題
			String post_title = JsonUtils.toJson(String.valueOf(mapList.get(i).get("title")));
			//獲取發佈時間
			String post_modified = JsonUtils.toJson(String.valueOf(mapList.get(i).get("pubDate")));
			//獲取發佈內容
			String post_content = JsonUtils.toJson(String.valueOf(mapList.get(i).get("description")));
			//獲取文章路徑 y根據文章的id來定義
			String guid = JsonUtils.toJson("https://www.simonjia.top/?p=" + y);
			y++;
			String sql = "INSERT INTO `word_press`.`wp_posts`(id,guid,post_author,post_date,post_date_gmt,post_content,post_title,`post_excerpt`," +
					"`post_status`, `comment_status`, `ping_status`, `post_name`,`to_ping`, `pinged`,   `post_modified`, `post_modified_gmt`, `post_content_filtered`,`post_parent`,`menu_order`, `post_type`, `post_mime_type`, `comment_count` )VALUES(" +
					+y + "," + guid + ",1," + post_modified + "," + post_modified + "," + post_content + "," + post_title + ",'','publish','open', 'open'," + post_title + ",'', '', " + post_modified + "," + post_modified + "," + post_content + ",0," +
					"0, 'post', '', 0);";
			//默認給文章分到新目錄
//			String sql = "insert into wp_term_relationships values("+y+",1,0);";
			sql = sql.replaceAll("\t", "");
			System.out.println("--------sq;------" + sql);
			bw.write(sql + "\t\n");
		}
		//注意關閉的先後順序,先打開的後關閉,後打開的先關閉
		bw.close();
		osw.close();
		fos.close();
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章