简体   繁体   English

使用 lxml 在 Python 中格式化 xml 文件

[英]Pretty formatting xml file in Python using lxml

I am trying to add a vhost entry to tomcat server.xml using python lxml我正在尝试使用 python lxml 向 tomcat server.xml 添加一个 vhost 条目

import io
from lxml import etree

newdoc = etree.fromstring('<Host name="getrailo.com" appBase="webapps"><Context path=""    docBase="/var/sites/getrailo.org" /><Alias>www.getrailo.org</Alias><Alias>my.getrailo.org</Alias></Host>')
doc = etree.parse('/root/server.xml')
root = doc.getroot()
for node1 in root.iter('Service'):
        for node2 in node1.iter('Engine'):
                node2.append(newdoc)
doc.write('/root/server.xml')

The problem is that it is removing the <?xml version='1.0' encoding='utf-8'?>问题是它正在删除<?xml version='1.0' encoding='utf-8'?>

line on top of the file from the output and the vhost entry is all in one line .How can I add the xml element in a pretty way like来自输出的文件顶部的行和 vhost 条目都在一行中。如何以一种漂亮的方式添加 xml 元素,例如

<Host name="getrailo.org" appBase="webapps">
         <Context path="" docBase="/var/sites/getrailo.org" />
         <Alias>www.getrailo.org</Alias>
         <Alias>my.getrailo.org</Alias>
</Host>

First you need to parse existing file with remove_blank_text so that it's clean and with no extra spaces that I think is a problem in this case首先,您需要使用remove_blank_text解析现有文件,使其干净并且没有我认为在这种情况下有问题的多余空格

parser = etree.XMLParser(remove_blank_text=True)
newdoc = etree.fromstring('/root/server.xml' parser=parser)

Then you're safe to write it back to disk with pretty_print and xml_declaration set in doc.write()然后,您可以安全地使用doc.write()设置的pretty_printxml_declaration将其写回磁盘

doc.write('/root/server.xml',  
          xml_declaration=True, 
          encoding='utf-8', 
          pretty_print=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM