2011-12-31 144 views
1

我需要手动添加我的xml版本作为字符串,所以我需要删除由ElementTree生成的xml版本。如何删除第3行的重复xml版本节点?使用Python从ElementTree中删除XML版本标记

<?xml version="1.0" encoding="utf-8"?> 
<!DOCTYPE smil PUBLIC "-//W3C//DTD SMIL 2.0//EN" "http://www.w3.org/2001/SMIL20/SMIL20.dtd"> 
<?xml version="1.0" encoding="utf-8"?> 
<smil xmlns="http://www.w3.org/2001/SMIL20/Language"> 
    <head> 
    <meta base="rtmp://cp23636.edgefcs.net/ondemand"/> 
    </head> 
    <body> 
    <switch> 
     <video src="mp4:soundcheck/1/clay_aiken/02_sc_ca_sorry_256.mp4" system-bitrate="336000"/> 
     <video src="mp4:soundcheck/1/clay_aiken/02_sc_ca_sorry_512.mp4" system-bitrate="592000"/> 
     <video src="mp4:soundcheck/1/clay_aiken/02_sc_ca_sorry_768.mp4" system-bitrate="848000"/> 
     <video src="mp4:soundcheck/1/clay_aiken/02_sc_ca_sorry_1128.mp4" system-bitrate="1208000"/> 
    </switch> 
    </body> 
</smil> 

这是脚本;

def prettify(elem): 
    """Return a pretty-printed XML string for the Element. 
    """ 
    rough_string = ElementTree.tostring(elem, 'utf-8') 
    reparsed = minidom.parseString(rough_string) 
    return reparsed.toprettyxml(indent=" ", encoding = 'utf-8') 

xmlver = '<?xml version="1.0" encoding="utf-8"?>\n' 
doctype = '<!DOCTYPE smil PUBLIC "-//W3C//DTD SMIL 2.0//EN" "http://www.w3.org/2001/SMIL20/SMIL20.dtd">\n' 

with open(sys.argv[1], 'rU') as f: 
    reader = csv.DictReader(f) 
    for row in reader: 
     root = Element('smil') 
     root.set('xmlns', 'http://www.w3.org/2001/SMIL20/Language') 
     head = SubElement(root, 'head') 
     meta = SubElement(head, 'meta base="rtmp://cp23636.edgefcs.net/ondemand"') 
     body = SubElement(root, 'body') 

     switch_tag = ElementTree.SubElement(body, 'switch') 

     for suffix, bitrate in video_data: 
      attrs = {'src': ("mp4:soundcheck/{year}/{id}/{file_root_name}_{suffix}.mp4" 
          .format(suffix=str(suffix), **row)), 
        'system-bitrate': str(bitrate), 
        } 
      ElementTree.SubElement(switch_tag, 'video', attrs) 

回答

2

请通过ElemenTtree生成的XML处理指令,并插入一个doctype节点,到minidom文件:

def prettify(elem, doctype=None): 
    """Return a pretty-printed XML string for the Element. 
    """ 
    rough_string = ElementTree.tostring(elem, 'utf-8') 
    reparsed = minidom.parseString(rough_string) 
    if doctype is not None: 
     reparsed.insertBefore(doctype, reparsed.documentElement) 
    return reparsed.toprettyxml(indent=" ", encoding = 'utf-8') 

doctype = minidom.getDOMImplementation('').createDocumentType(
    'smil', '-//W3C//DTD SMIL 2.0//EN', 
    'http://www.w3.org/2001/SMIL20/SMIL20.dtd') 
+0

虽然这个答案解决了眼前的问题,请参见[我的回答(HTTP:// stackoverflow.com/a/8690365/984421)转到您关于此主题的其他问题以获得更完整的解决方案。 – ekhumoro 2011-12-31 20:27:57