本文目录导读:

- 解析
.xmind文件(XMind 8 / XMind 2020+) - 解析
.mm文件(FreeMind / Freeplane) - 解析
.opml文件(大纲格式,跨软件通用) - 解析 Markdown 大纲格式(如 Markmap)
- 解析 JSON 格式的思维导图(如 SimpleMind 或自定义)
- 通用解析建议
解析思维导图文件通常需要根据文件格式(如 .xmind、.mm、.opml、.txt 等)选择对应的解析库或手动解析数据结构。
以下是几种常见思维导图文件格式的解析方法(以 Python 为例):
解析 .xmind 文件(XMind 8 / XMind 2020+)
XMind 文件本质上是 ZIP 压缩包,内部包含 XML 文件。
使用 xmindparser 库(推荐,最简单)
from xmindparser import xmind_to_dict
# 解析为字典列表
result = xmind_to_dict('your_file.xmind')
# 通常结构:result[0]['topic']['topics']
sheet = result[0] # 第一个画布
root_topic = sheet['topic']
print(root_topic['title'])
print(root_topic['topics']) # 子主题列表
手动解压+解析 XML(或使用 xmind 库)
import zipfile
import xml.etree.ElementTree as ET
with zipfile.ZipFile('your_file.xmind', 'r') as z:
# XMind 8 主要内容在 content.xml
with z.open('content.xml') as f:
tree = ET.parse(f)
root = tree.getroot()
# 解析 XML 节点,查找 topic 标签
for topic in root.iter('{urn:xmind:xmap:xmlns:content:2.0}topic'):
title = topic.find('{urn:xmind:xmap:xmlns:content:2.0}title')
if title is not None:
print(title.text)
解析 .mm 文件(FreeMind / Freeplane)
.mm 文件是标准的 XML 格式。
import xml.etree.ElementTree as ET
tree = ET.parse('your_file.mm')
root = tree.getroot()
def parse_node(node, depth=0):
text = node.get('TEXT', '') # FreeMind 使用 TEXT 属性
print(' ' * depth + text)
for child in node.findall('node'):
parse_node(child, depth+1)
# 根节点通常是 map 的子元素
map_node = root.find('node')
if map_node is not None:
parse_node(map_node)
解析 .opml 文件(大纲格式,跨软件通用)
.opml 是 XML 格式,常用于思维导图交换。
import xml.etree.ElementTree as ET
tree = ET.parse('your_file.opml')
root = tree.getroot()
def parse_outline(element, depth=0):
text = element.get('text', element.get('title', ''))
print(' ' * depth + text)
for child in element.findall('outline'):
parse_outline(child, depth+1)
# 查找 body 下的 outline
body = root.find('body')
if body is not None:
for outline in body.findall('outline'):
parse_outline(outline)
解析 Markdown 大纲格式(如 Markmap)
简单的文本缩进结构:
import re
with open('mindmap.md', 'r') as f:
content = f.read()
lines = content.split('\n')
for line in lines:
if line.strip().startswith('#'):
# 标题层级
level = len(line.split(' ')[0]) # # 的数量
title = line.strip().lstrip('#').strip()
print(f'Level {level}: {title}')
elif line.strip().startswith('-') or line.strip().startswith('*'):
# 列表项
indent = len(line) - len(line.lstrip())
level = indent // 2 + 1 # 假设每2空格一层
title = line.strip().lstrip('-* ').strip()
print(f'Level {level}: {title}')
解析 JSON 格式的思维导图(如 SimpleMind 或自定义)
import json
with open('your_file.json', 'r') as f:
data = json.load(f)
def parse_node(node, depth=0):= node.get('title', node.get('name', node.get('text', '')))
print(' ' * depth + title)
children = node.get('children', [])
for child in children:
parse_node(child, depth+1)
parse_node(data)
通用解析建议
| 文件扩展名 | 实际格式 | 推荐解析方式 |
|---|---|---|
.xmind |
ZIP(内含XML) | xmindparser 库 |
.mm |
XML | xml.etree 或 lxml |
.opml |
XML | xml.etree |
.km |
JSON | json 库 |
.txt |
缩进文本 | 正则 / 行解析 |
.md |
Markdown | markdown 库 + 树构建 |
- 判断文件格式:看扩展名或文件头魔数(ZIP 文件以
PK开头,XML 以<开头)。 - 解压/读取:ZIP 就解压,XML/JSON 直接读取。
- 定位节点:思维导图本质是树结构,找到每个节点的标题和子节点列表。
- 遍历递归:用递归或栈遍历整棵树。
如果你有具体的文件格式或编程语言需求,可以进一步说明,我可以给出更精确的代码示例。