python-xml-拼接
from xml.etree import ElementTree as ET
import xml
def joinXmlElement(**kwargs):
"""
根据输入的字典数据 生成xmlElement 对象
:return:
"""
element = kwargs.get("element", None)
data = kwargs['data']
root = kwargs.get("root", {"tag": "Value"})
if element is None:
element = ET.Element(root["tag"], attrib=root.get("attribute", {}))
for tag, value in data.items():
tag = tag.replace("#", "")
attribute, text, child = value.get("attribute", {}), value.get("text", None), value.get("child", None)
subElement = ET.SubElement(element, tag, attrib=attribute)
if text is not None:
subElement.text = text
if child is not None:
joinXmlElement(element=subElement, data=child)
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)