python(13) : json数据生成xmind软件可导入的文件

来自: 豆包

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值