ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python xml漂亮的打印不工作

2019-06-29 11:46:39  阅读:252  来源: 互联网

标签:python pretty-print xml lxml


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

解决方法:

取自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)

标签:python,pretty-print,xml,lxml
来源: https://codeday.me/bug/20190629/1325912.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有