5

我通过从列表中添加一些节点和值来更改一些 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 一起工作?

4

2 回答 2

3

取自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)
于 2015-02-06T06:37:21.970 回答
0

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

from StringIO import StringIO
from lxml import etree
parser = etree.XMLParser(remove_blank_text=True)
tree = etree.parse(StringIO(templateXml), parser)
于 2013-08-12T04:46:44.827 回答