使用python在xml中添加元素

问题描述

如何将元素添加到我的属性中? 我知道如何添加文本:

ET.subElement(root[2],'resource')
for x in root.iter('resource'):

但我正在寻找的是:

<resources>
        <resource identifier="presentation_3" type="webcontent" href="questions.html" adlcp:scormtype="sco">
            <file href="questions.html"/>
        </resource>
    </resources>

下面是我的代码

import xml.etree.ElementTree as ET

xmlfile = "imsmanifest.xml"

tree = ET.parse(xmlfile)
root = tree.getroot()

ET.SubElement(root[2],'file').set('href','index.html')
tree.write('new.xml')

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)