导出博客园的内容并生成markdown文件
导出博客园的内容并生成markdown文件
作者:Grey
博客园支持备份功能:
操作时间是:
工作日18:00之后、8点之前或周六、周日进行备份。
点击备份,可以选择时间段,导出以后,是xml格式,样例格式如下:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
<channel>
<title>博客园-xxx</title>
<link>https://www.cnblogs.com/xxx</link>
<description>xxxx</description>
<language>zh-cn</language>
<lastBuildDate>Mon, 28 Jun 2021 12:48:06 GMT</lastBuildDate>
<pubDate>Mon, 28 Jun 2021 12:48:06 GMT</pubDate>
<ttl>60</ttl>
<item>
<title>文章3</title>
<link>http://www.baidu.com</link>
<dc:creator>作者名称</dc:creator>
<author>作者名称</author>
<pubDate>Mon, 21 Jun 2021 14:01:00 GMT</pubDate>
<guid>http://www.baidu.com</guid>
<description><![CDATA[这里是正文信息]]></description>
</item>
<item>
<title>文章1</title>
<link>http://www.baidu.com</link>
<dc:creator>作者名称</dc:creator>
<author>作者名称</author>
<pubDate>Mon, 21 Jun 2021 14:01:00 GMT</pubDate>
<guid>http://www.baidu.com</guid>
<description><![CDATA[这里是正文信息]]></description>
</item>
<item>
<title>文章2</title>
<link>http://www.baidu.com</link>
<dc:creator>作者名称</dc:creator>
<author>作者名称</author>
<pubDate>Mon, 21 Jun 2021 14:01:00 GMT</pubDate>
<guid>http://www.baidu.com</guid>
<description><![CDATA[这里是正文信息]]></description>
</item>
</channel>
</rss>
可以通过解析xml,拿到每篇文章的内容标题,并且生成markdown文档,代码如下:
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.text.ParseException;
import java.util.Date;
import java.util.Iterator;
/**
* 博客园xml转md文档
*/
public class App {
// 指定你要导出的目录
static final String path = "C:\\Users\\zhuiz\\OneDrive\\blogs";
public static void main(String[] args) throws DocumentException, IOException, ParseException {
SAXReader reader = new SAXReader();
URL url = App.class.getClassLoader().getResource("sample.xml");
Document document = reader.read(url);
Element root = document.getRootElement();
Element channel = null;
for (Iterator<Element> it = root.elementIterator(); it.hasNext(); ) {
channel = it.next();
}
for (Iterator<Element> it = channel.elementIterator("item"); it.hasNext(); ) {
Element item = it.next();
String title = item.element("title").getTextTrim();
String link = item.element("link").getTextTrim();
String prefix = "---\n" +
"title: '" + title + "'\n" +
"date: " + format(item.element("pubDate").getTextTrim()) + "\n\n\n\n" +
"---\n\n\n\n" +
"<meta name = \"referrer\" content = \"no-referrer\" />\n\n\n\n";
inputFile(title(title), link + "\n\n\n\n\n" + prefix + item.element("description").getText());
}
}
// 替换windows中非法文件名
public static String title(String invalidTitle) {
return invalidTitle.replaceAll("[/\\\\:*?<>|]", "") + ".md";
}
public static void inputFile(String title, String content) throws IOException {
File file = new File(path, title);
file.createNewFile();
FileUtil.appendString(content, file, "UTF-8");
}
static String format(String stringDate) {
return DateUtil.format(new Date(stringDate), "yyyy-MM-dd HH:mm:ss");
}
}
使用说明:
- path指定你要导出的markdown文件放到哪里
- 用户导出的xml文件重命名成sample.xml放在resources文件夹中
- 运行App.java
源码地址:
待完善:
-
处理重名博客。
-
代码可以重构一下。
作者:GreyZeng
出处:https://www.cnblogs.com/greyzeng/p/14949545.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
你可以在这里自定义其他内容
本文来自博客园,作者:Grey Zeng,转载请注明原文链接:https://www.cnblogs.com/greyzeng/p/14949545.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了