【问题标题】:Remove XML Version Tag from ElementTree using Python使用 Python 从 ElementTree 中删除 XML 版本标记
【发布时间】:2011-12-31 00:06:57
【问题描述】:

我需要手动将我的 xml 版本添加为字符串,因此我需要删除 ElementTree 生成的 xml 版本。如何删除第 3 行重复的 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)

【问题讨论】:

    标签: python xml elementtree


    【解决方案1】:

    保留ElemenTtree生成的xml处理指令,在minidom文档中插入一个doctype节点:

    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')
    

    【讨论】:

    • 虽然这个答案解决了眼前的问题,但请参阅my answer 了解有关此主题的其他问题以获得更完整的解决方案。
    猜你喜欢
    • 2018-04-12
    • 2016-09-17
    • 2011-08-22
    • 2015-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多