栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python-xml-拼接

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python-xml-拼接

python-xml-拼接
from xml.etree import ElementTree as ET
import xml

def joinXmlElement(**kwargs):
    """
    根据输入的字典数据 生成xmlElement 对象

    :return:
    """
    # subElement = ET.SubElement(element, "Array", attrib={'Qty': f"{len(subData):>04}"}) #


    element = kwargs.get("element", None)
    data = kwargs['data'] # 拼接的数据

    root = kwargs.get("root", {"tag": "Value"})
    # 父节点 Value
    if element is None:
        element = ET.Element(root["tag"], attrib=root.get("attribute", {}))
    for tag, value in data.items():
        # 添加特殊字符,解决重复tag
        tag = tag.replace("#", "")
        attribute, text, child = value.get("attribute", {}),  value.get("text", None), value.get("child", None)
        # #  
        # 添加子节点
        subElement = ET.SubElement(element, tag, attrib=attribute)
        # 如果 text 不为空则添加文本
        # #  text
        if text is not None:
            subElement.text = text
        # 如果有字节点,则递归调用
        if child is not None:
            joinXmlElement(element=subElement, data=child)
    # 生成xml 文件 方便查看,可注释
    element = ET.ElementTree(element)
    if isinstance(element, ET.ElementTree):
        element.write('result.xml', encoding='utf-8')
    return element



###### 示例
data = {"jenkins.plugins.http__request.HttpRequest": {"attribute":{"plugin": "http_request@1.9.0"}},
"url": {"text": "Test"},
"ignoreSslErrors": {"text": "false"}}
response = joinXmlElement(data=data)
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/360616.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号