【问题标题】:XML Declaration standalone="yes" lxmlXML 声明独立 =“是”lxml
【发布时间】:2013-08-12 23:55:21
【问题描述】:

我正在解析一个 xml,进行一些更改并保存到一个新文件中。它有我想保留的声明<?xml version="1.0" encoding="utf-8" standalone="yes"?>。当我保存新文件时,我丢失了standalone="yes" 位。我怎样才能把它留在里面? 这是我的代码:

templateXml = """<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<package>
  <provider>Some Data</provider>
  <studio_display_name>Some Other Data</studio_display_name>
</package>"""

from lxml import etree
tree = etree.fromstring(templateXml)

xmlFileOut = '/Users/User1/Desktop/Python/Done.xml'   

with open(xmlFileOut, "w") as f:
    f.write(etree.tostring(tree, pretty_print = True, xml_declaration = True, encoding='UTF-8'))

【问题讨论】:

    标签: python xml parsing xml-parsing lxml


    【解决方案1】:

    您可以将standalone 关键字参数传递给tostring()

    etree.tostring(tree, pretty_print = True, xml_declaration = True, encoding='UTF-8', standalone=True)
    

    【讨论】:

    • TypeError: tostring() got an unexpected keyword argument 'xml_declaration'
    • @ArnoldRoa 你在用lxml.etree吗?
    【解决方案2】:

    如果您想完全禁用输出standalone,请通过None 而不是TrueFalse。听起来合乎逻辑,但实际找到并测试它需要一些时间。

    etree.tostring(tree, xml_declaration = True, encoding='utf-8', standalone=None)
    

    或使用上下文管理器和流etree.xmlfile 序列化:

    with etree.xmlfile(open('/tmp/test.xml'), encoding='utf-8') as xf:
        xf.write_declaration(standalone=None)
        with xf.element('html'):
            with xf.element('body'):
                element = etree.Element('p')
                element.text = 'This is paragraph'
                xf.write(element)
    

    【讨论】:

      【解决方案3】:
      etree.tostring(tree, pretty_print = True, xml_declaration = True, encoding='UTF-8')
      

      如果您使用 lxml,将添加声明,但是我注意到他们的声明使用半引号而不是全引号。

      您还可以通过将输出与您需要的静态字符串连接起来获得您想要的确切声明:

      xml = etree.tostring(tree, pretty_print = True, encoding='UTF-8')
      xml = '<?xml version=\"1.0\" encoding=\"utf-8\"?>\n' + xml
      

      【讨论】:

        【解决方案4】:

        如果您想在 XML 标头中显示 standalone='no' 参数,则必须将其设置为 False 而不是“否”。就像这样:

        etree.tostring(tree, pretty_print = True, xml_declaration = True, encoding='UTF-8', standalone=False)
        

        如果不是,standalone 将默认设置为“yes”。

        【讨论】:

          【解决方案5】:

          使用tree.docinfo.standalone 指定standalone

          尝试以下操作:

          from lxml import etree
          tree = etree.fromstring(templateXml).getroottree() # NOTE: .getroottree()
          
          xmlFileOut = '/Users/User1/Desktop/Python/Done.xml'   
          
          with open(xmlFileOut, "w") as f:
              f.write(etree.tostring(tree, pretty_print=True, xml_declaration=True,
                                     encoding=tree.docinfo.encoding,
                                     standalone=tree.docinfo.standalone))
          

          【讨论】:

          • 抱歉,您的回答很有魅力,我只是觉得@alecxe 的回答对我来说更容易实现,无论如何感谢您的回答,有选项很好。
          • @user2446702,好的,我明白了。
          • 不,不行。这是不发明数据的答案。
          • @juanitogan,对不起,我不明白你的意思。能否提供更多信息?
          • @falsetru - 我的意思是,OP 选择另一个答案是正确的是不行的。你的更正确,因为它不会用硬编码值覆盖输入值。 [这你知道——我只是在和其他人交谈。] 另一个答案更容易实现,只是因为它在一行上需要更少的击键,而且只是懒惰的编程更容易在某些时候导致问题。
          猜你喜欢
          • 2017-11-05
          • 1970-01-01
          • 2012-01-16
          • 1970-01-01
          • 1970-01-01
          • 2010-12-13
          • 2018-07-25
          • 1970-01-01
          • 2016-10-28
          相关资源
          最近更新 更多