Python 使用minidom创建XML文件

Python 使用minidom创建XML文件,python,xml,minidom,Python,Xml,Minidom,我想用python创建一个xml文件,格式如下: <datasource formatted-name='federated' inline='true'> <connection class='federated'> <named-connections> <named-connection caption='Sample - Superstore' name='excel.1ew9u4t0tggb9315darmm0nfz2k

我想用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>
我只得到这个

<?xml version="1.0" ?>
<Database>
    <Connection name="Federated"/>
    named-connections
</Database>

命名连接
我不知道如何在
中创建子节点。有人能帮忙吗

谢谢,
阿鲁什

我也在找同样的东西。我一直在寻找答案。如果其他人需要相同的信息:

root = minidom.Document() 
 
xml = root.createElement('Datasource')  
root.appendChild(xml) 

productChild = root.createElement('connection')
productChild.setAttribute('formatted-name', 'Federated') 
productChild.setAttribute('inline', 'true') 
xml.appendChild(productChild)

# named-connections indent
childOfProduct = root.createElement('named-connections')
productChild.appendChild(childOfProduct)

# named-connection indent
productChild1 = root.createElement('named-connection')
productChild1.setAttribute('caption', 'Sample - Superstore')
productChild1.setAttribute('name', 'excel.1ew9u4t0tggb9315darmm0nfz2kb')
childOfProduct.appendChild(productChild1)

# connection indent
productChild2 = root.createElement('connection')
productChild2.setAttribute('class', 'excel')
productChild2.setAttribute('driver', '')
productChild2.setAttribute('header', 'yes')
productChild2.setAttribute('imex', '1')
productChild2.setAttribute('password', '')
productChild2.setAttribute('server', '')
productChild1.appendChild(productChild2)


对不起,如果有什么不对劲,我不是专业人士。
希望这对未来的搜索者有所帮助。

这是正确的答案。@A你应该接受它。