【问题标题】:python xml pretty print not workingpython xml漂亮的打印不起作用
【发布时间】:2013-08-13 03:49:06
【问题描述】:

我通过从列表中添加一些节点和值来更改一些 xml。我可以成功创建所有新标签和值,我在贡献者标签之间创建它们,但是当我将 xml 保存到一个新文件时,我创建的标签都在一行上。这是我的代码示例:

templateXml = """<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<package>
  <delivery_type>new</delivery_type>
  <feature>
    <feature_type>Movie</feature_type>
    <contributors>
    </contributors>
</package>"""

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

node_video = tree.xpath('//feature/contributors')[0]
for cast in castList:
    pageElement = etree.SubElement(node_video, 'contributor')
    node_video1 = tree.xpath('//feature/contributors/contributor')[0]
    pageElement.attrib['type'] = 'cast'
    pageElement1 = etree.SubElement(pageElement, 'name')
    pageElement1.text = cast.text
    pageElement2 = etree.SubElement(pageElement, 'role')
    pageElement2.text = "actor"

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', standalone="yes"))

这里是保存的xml文件:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<package>
  <delivery_type>new</delivery_type>
  <feature>
    <feature_type>Movie</feature_type>
    <contributors>
    <contributor type="cast"><name>John Doe</name><role>actor</role></contributor><contributor type="cast"><name>Another Actors name</name><role>actor</role></contributor><contributor type="cast"><name>Jane Doe</name><role>actor</role></contributor><contributor type="cast"><name>John Smith</name><role>actor</role></contributor></contributors>
</package>

我在使用以下代码打开 xml 文件时解决了这个问题:

from lxml import etree
parser = etree.XMLParser(remove_blank_text=True) # makes pretty print work
path3 = 'path_to_xml_file'
open(path3)
tree = etree.parse(path3, parser)
root = tree.getroot()
tree.write(xmlFileOut, pretty_print = True, xml_declaration = True, encoding = 'UTF-8')

这可行,但我如何让它与字符串 xml 一起工作?

【问题讨论】:

    标签: python xml lxml pretty-print


    【解决方案1】:

    一个简单的解决方案可能是使用 StringIO:

    from StringIO import StringIO
    from lxml import etree
    parser = etree.XMLParser(remove_blank_text=True)
    tree = etree.parse(StringIO(templateXml), parser)
    

    【讨论】:

      【解决方案2】:

      取自http://ruslanspivak.com/2014/05/12/how-to-pretty-print-xml-with-lxml/

      import StringIO
      import lxml.etree as etree
      
      def prettify(xml_text):
          """Pretty prints xml."""
          parser = etree.XMLParser(remove_blank_text=True)
          file_obj = StringIO.StringIO(xml_text)
          tree = etree.parse(file_obj, parser)
          return etree.tostring(tree, pretty_print=True)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-03-25
        • 2017-12-19
        • 2018-10-04
        • 1970-01-01
        相关资源
        最近更新 更多