我想用python创建一个xml文件,格式如下:
<datasource formatted-name='federated' inline='true'>
<connection class='federated'>
<named-connections>
<named-connection caption='Sample - Superstore' name='excel.1ew9u4t0tggb9315darmm0nfz2kb'>
<connection class='excel' driver='' header='yes' imex='1' password='' server='' />
</named-connection>
</named-connections>
</connection>
</datasource>
我试过用minidom
from xml.dom import minidom
import os
root = minidom.Document()
xml = root.createElement('Datasource')
root.appendChild(xml)
productChild = root.createElement('Connection')
productChild.setAttribute('name', 'Federated')
xml.appendChild(productChild)
productChild1 = root.createTextNode('named-connections')
productChild2=root.createElement('named-connection')
productChild2.setAttribute('name', 'caption=SampleSuperstore')
xml.appendChild(productChild1)
xml_str = root.toprettyxml(indent ="\t")
save_path_file = "gfg.xml"
with open(save_path_file, "w") as f:
f.write(xml_str)
我只得到这个
<?xml version="1.0" ?>
<Database>
<Connection name="Federated"/>
named-connections
</Database>
我不知道如何在<named-connections>
内创建子节点。有人能帮忙吗
谢谢, 阿鲁什
我也在找同样的东西。我一直在寻找答案。如果其他人需要相同的信息:
对不起,如果有什么不对劲,我不是专业人士。 希望这对未来的搜索者有所帮助
相关问题 更多 >
编程相关推荐