来自: 豆包

Xmind导入说明: 随便打开一个xmind文件, 然后选择"文件"-"导入"-"OPML", 然后保存为.xmind文件即可

import xml.etree.ElementTree as ET


def json_to_opml(json_data, title_str):
    opml = ET.Element('opml', version='2.0')
    head = ET.SubElement(opml, 'head')
    title = ET.SubElement(head, 'title')
    title.text = title_str
    body = ET.SubElement(opml, 'body')
    build_outline(body, json_data)
    return opml


def build_outline(parent, data):
    for item in data['children'] if 'children' in data else []:
        outline = ET.SubElement(parent, 'outline', text=item['name'])
        if 'children' in item:
            build_outline(outline, item)


data = {
    "name": "无效",
    "children": [
        {
            "name": "地球",
            "children": [
                {
                    "name": "非洲",
                    "children": [
                        {
                            "name": "阿尔及利亚",
                            "children": [
                                {
                                    "name": "阿德拉尔省",
                                    "children": [
                                        {
                                            "name": "Aoulef市",
                                            "children": []
                                        },
                                        {
                                            "name": "Akabli市",
                                            "children": []
                                        }
                                    ]
                                },
                                {
                                    "name": "瓦赫兰省",
                                    "children": []
                                }
                            ]
                        },
                        {
                            "name": "安哥拉",
                            "children": [
                                {
                                    "name": "本戈省",
                                    "children": []
                                },
                                {
                                    "name": "本格拉省",
                                    "children": []
                                }
                            ]
                        }
                    ]
                },
                {
                    "name": "欧洲",
                    "children": [
                        {
                            "name": "法国",
                            "children": []
                        },
                        {
                            "name": "英国",
                            "children": []
                        }
                    ]
                }
            ]
        }
    ]
}

opml_data = json_to_opml(data, '地理区域信息')

# 保存为 OPML 文件
ET.ElementTree(opml_data).write('output.opml', encoding='utf-8', xml_declaration=True)

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐