Python minidom xml文件创建,获取额外标签

时间:2015-04-13 11:40:53

标签: python xml python-2.7

创建Python minidom xml文件,获取额外标签 来自以下代码段

import os, glob
import xml.dom
import xml.dom.minidom    

doc = xml.dom.minidom.Document()
url = ""
rootelt = doc.createElementNS(url, "txtLyr")
doc.appendChild(rootelt)
i = 1
for l in layers:
    if pdb.gimp_drawable_is_text_layer(l):
        xmlnode = doc.createElementNS(url, "txtLyr_" + active_dgm)
        lbl = "txtLayerLbl" + str(i)
        xmlnode.setAttributeNS(url, "txtLyrALbl", "%s"%lbl)
        xmlnode.setAttributeNS(url, "txtLyrFSize", "%.1f%d"%pdb.gimp_text_layer_get_font_size(l))
        xmlnode.setAttributeNS(url, "txtLyrFClr", "%s"%pdb.gimp_text_layer_get_color(l))
        xmlnode.setAttributeNS(url, "txtLyrStr", "%s"%l.name)
        xmlnode.setAttributeNS(url, "txtLyrX", "%d"%l.offsets[0])
        xmlnode.setAttributeNS(url, "txtLyrY", "%d"%l.offsets[1])
        xmlnode.setAttributeNS(url, "txtLyrW", "%d"%l.width)
        xmlnode.setAttributeNS(url, "txtLyrH", "%d"%l.height)
        i = i + 1
        xmltext = doc.createTextNode("");
        xmlnode.appendChild(xmltext)
        rootelt.appendChild(xmlnode)

gimp.pdb.gimp_message( "Saving to %s"%(outdir+wcname) );
file_object = open(outdir+os.sep+wcname, "w")
file_object.write(doc.toprettyxml(indent="    "));
file_object.close()

我明白了,添加“>”的是什么我只想要“/>”。

<?xml version="1.0" ?>
<txtLyr>
    <txtLyr_a2a txtLyrALbl="txtLayerLbl1" txtLyrFClr="RGB (0.0, 0.0, 0.0, 1.0)" txtLyrFSize="18.00" txtLyrH="22" txtLyrStr="Test 1" txtLyrW="52" txtLyrX="290" txtLyrY="122"></txtLyr_a2a>
    <txtLyr_a2a txtLyrALbl="txtLayerLbl2" txtLyrFClr="RGB (0.0, 0.0, 0.0, 1.0)" txtLyrFSize="18.00" txtLyrH="22" txtLyrStr="Test 2" txtLyrW="52" txtLyrX="302" txtLyrY="390"></txtLyr_a2a>
    <txtLyr_a2a txtLyrALbl="txtLayerLbl3" txtLyrFClr="RGB (0.0, 0.0, 0.0, 1.0)" txtLyrFSize="18.00" txtLyrH="22" txtLyrStr="Test 3" txtLyrW="52" txtLyrX="261" txtLyrY="738"></txtLyr_a2a>
    <txtLyr_a2a txtLyrALbl="txtLayerLbl4" txtLyrFClr="RGB (0.0, 1.0, 0.0, 1.0)" txtLyrFSize="18.00" txtLyrH="22" txtLyrStr="Test 4" txtLyrW="52" txtLyrX="888" txtLyrY="131"></txtLyr_a2a>
    <txtLyr_a2a txtLyrALbl="txtLayerLbl5" txtLyrFClr="RGB (0.0, 1.0, 0.0, 1.0)" txtLyrFSize="44.00" txtLyrH="52" txtLyrStr="Test 5" txtLyrW="129" txtLyrX="903" txtLyrY="356"></txtLyr_a2a>
    <txtLyr_a2a txtLyrALbl="txtLayerLbl6" txtLyrFClr="RGB (1.0, 0.0, 0.0, 1.0)" txtLyrFSize="44.00" txtLyrH="50" txtLyrStr="Test 6" txtLyrW="103" txtLyrX="929" txtLyrY="635"></txtLyr_a2a>
</txtLyr>

我想知道这件事,无法弄清楚我错过了什么或在这里添加,谢谢

<?xml version="1.0" ?>
<txtLyr>
    <txtLyr_a2a txtLyrALbl="txtLayerLbl1" txtLyrFClr="RGB (0.0, 0.0, 0.0, 1.0)" txtLyrFSize="18.00" txtLyrH="22" txtLyrStr="Test 1" txtLyrW="52" txtLyrX="290" txtLyrY="122" />
    <txtLyr_a2a txtLyrALbl="txtLayerLbl2" txtLyrFClr="RGB (0.0, 0.0, 0.0, 1.0)" txtLyrFSize="18.00" txtLyrH="22" txtLyrStr="Test 2" txtLyrW="52" txtLyrX="302" txtLyrY="390" />
    <txtLyr_a2a txtLyrALbl="txtLayerLbl3" txtLyrFClr="RGB (0.0, 0.0, 0.0, 1.0)" txtLyrFSize="18.00" txtLyrH="22" txtLyrStr="Test 3" txtLyrW="52" txtLyrX="261" txtLyrY="738" />
    <txtLyr_a2a txtLyrALbl="txtLayerLbl4" txtLyrFClr="RGB (0.0, 1.0, 0.0, 1.0)" txtLyrFSize="18.00" txtLyrH="22" txtLyrStr="Test 4" txtLyrW="52" txtLyrX="888" txtLyrY="131" />
    <txtLyr_a2a txtLyrALbl="txtLayerLbl5" txtLyrFClr="RGB (0.0, 1.0, 0.0, 1.0)" txtLyrFSize="44.00" txtLyrH="52" txtLyrStr="Test 5" txtLyrW="129" txtLyrX="903" txtLyrY="356" />
    <txtLyr_a2a txtLyrALbl="txtLayerLbl6" txtLyrFClr="RGB (1.0, 0.0, 0.0, 1.0)" txtLyrFSize="44.00" txtLyrH="50" txtLyrStr="Test 6" txtLyrW="103" txtLyrX="929" txtLyrY="635" />
</txtLyr>

1 个答案:

答案 0 :(得分:1)

这是因为您将空文本节点添加为<txtLyr_a2a>元素的内容。删除以下两行以获得自动关闭标记:

xmltext = doc.createTextNode("");
xmlnode.appendChild(xmltext)