Python 将Minidom元素插入到xml中
我在将数据结构插入xml文档时遇到了一些问题,但没有大的成功Python 将Minidom元素插入到xml中,python,xml,minidom,Python,Xml,Minidom,我在将数据结构插入xml文档时遇到了一些问题,但没有大的成功 <?xml version="1.0" ?> <marl version="2.1" xmlns="xxxx.xsd"> <mcdata id="2" scope="all" type="plan"> <header> <log action="created"/> </header>
<?xml version="1.0" ?>
<marl version="2.1" xmlns="xxxx.xsd">
<mcdata id="2" scope="all" type="plan">
<header>
<log action="created"/>
</header>
<mObject class="foo" distName="a-1">
<p name="Ethernet">false</p>
<list name="pass"/>
</mObject>
<mObject class="bar" distName="a-1/b-2">
<p name="Voltage">false</p>
</mObject>
</mcdata>
</marl>
创建新节点并根据需要装饰它:
#create node <mObject>
element = document.createElement("mObject")
#add text content to the node
element.appendChild(document.createTextNode("content"))
#add attribute id to the node
element.setAttribute("id" , "foo")
#result: <mObject id="foo">content</mObject>
效果如何?行document=parse'mini.xml'向我生成一个解析错误:xml.parsers.expat.expat错误:未找到任何元素:第14行第0列对不起,我的错误。源xml格式不正确。问题现已解决。如果有帮助,请使用此代码元素=document.createElementmObject元素。appendChilddocument.createTextNodeHello文档。childNodes[0]。childNodes[1]。appendChildelement我作为的子级获得了Hello
#create node <mObject>
element = document.createElement("mObject")
#add text content to the node
element.appendChild(document.createTextNode("content"))
#add attribute id to the node
element.setAttribute("id" , "foo")
#result: <mObject id="foo">content</mObject>
#select a parent node
mc = document.getElementsByTagName("mcdata")[0]
#append the new node as child of the parent
mc.appendChild(element)