2011-12-31 58 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