加元在minidom命名蟒蛇属性 [英] add element with attributes in minidom python
本文介绍了加元在minidom命名蟒蛇属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要带属性的子节点添加到特定的标签。我的XML是
I want to add a child node with attributes to a specific tag. my xml is
<deploy>
</deploy>
和输出应该是
<deploy>
<script name="xyz" action="stop"/>
</deploy>
到目前为止,我的code是:
so far my code is:
dom = parse("deploy.xml")
script = dom.createElement("script")
dom.childNodes[0].appendChild(script)
dom.writexml(open(weblogicDeployXML, 'w'))
script.setAttribute("name", args.script)
我怎么能弄清楚如何找到部署标记并追加子节点与属性?
How can I figure out how to find deploy tag and append child node with attributes ?
推荐答案
<一个href=\"http://docs.python.org/2/library/xml.dom.html?highlight=setattribute#xml.dom.Element.setAttribute\"相对=nofollow> xml.dom.Element.setAttribute
xmlFile = minidom.parse( FILE_PATH )
for script in SCRIPTS:
newScript = xmlFile.createElement("script")
newScript.setAttribute("name" , script.name)
newScript.setAttribute("action", script.action)
newScriptText = xmlFile.createTextNode( script.description )
newScript.appendChild( newScriptText )
xmlFile.childNodes[0].appendChild( newScript )
print xmlFile.toprettyxml()
输出文件:
<?xml version="1.0" ?>
<scripts>
<script action="list" name="ls" > List a directory </script>
<script action="copy" name="cp" > Copy a file/directory </script>
<script action="move" name="mv" > Move a file/directory </script>
.
.
.
</scripts>
这篇关于加元在minidom命名蟒蛇属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文