示例

之前拿ET写xml,直接对root节点调用write函数,会出现产生的xml字符串没有缩进,是干巴巴的一行,可读性比较差,就像下面这样:

<annotation><filename>VW_CH3ENTERPRIZEVsTaurus_BO2_1_4_7.jpg</filename><size><width>168.81600000000006</width><height>121.66920000000009</height></size><object><name>Car</name><bndbox><xmin>14.068</xmin><ymin>10.139</ymin><xmax>154.748</xmax><ymax>111.53</ymax><width>140.68000000000006</width><height>101.39100000000008</height></bndbox></object><object><name>RedArmor3</name><bndbox><xmin>71.108</xmin><ymin>68.439</ymin><xmax>116.728</xmax><ymax>93.787</ymax><width>45.61999999999989</width><height>25.347999999999956</height></bndbox></object><object><name>RedArmor3</name><bndbox><xmin>17.878</xmin><ymin>59.567</ymin><xmax>44.488</xmax><ymax>87.45</ymax><width>26.6099999999999</width><height>27.883000000000038</height></bndbox></object><object><name>RedArmor3</name><bndbox><xmin>39.418</xmin><ymin>44.359</ymin><xmax>72.368</xmax><ymax>74.776</ymax><width>32.950000000000045</width><height>30.41700000000003</height></bndbox></object></annotation>

解决办法

看到一个不错的方法:
https://www.cnblogs.com/muffled/p/3462157.html
原理 先对root进行修饰,调用一个自定义的修饰函数

def indent(elem, level=0):
i = "\n" + level*"\t"
if len(elem):
if not elem.text or not elem.text.strip():
elem.text = i + "\t"
if not elem.tail or not elem.tail.strip():
elem.tail = i
for elem in elem:
indent(elem, level+1)
if not elem.tail or not elem.tail.strip():
elem.tail = i
else:
if level and (not elem.tail or not elem.tail.strip()):
elem.tail = i

之后的效果就会很棒:

<annotation>
<filename>VW_CH3ENTERPRIZEVsTaurus_BO2_1_5_4.jpg</filename>
<size>
<width>207</width>
<height>213</height>
</size>
<object>
<name>Car</name>
<bndbox>
<xmin>17.237</xmin>
<ymin>17.744</ymin>
<xmax>189.602</xmax>
<ymax>195.179</ymax>
<width>172.4</width>
<height>177.4</height>
</bndbox>
</object>
<object>
<name>RedArmor2</name>
<bndbox>
<xmin>140.173</xmin>
<ymin>128.007</ymin>
<xmax>173.125</xmax>
<ymax>152.087</ymax>
<width>33.0</width>
<height>24.1</height>
</bndbox>
</object>
<object>
<name>RedArmor2</name>
<bndbox>
<xmin>51.457</xmin><annotation>
<filename>VW_CH3ENTERPRIZEVsTaurus_BO2_1_5_4.jpg</filename>
<size>
<width>207</width>
<height>213</height>
</size>
<object>
<name>Car</name>
<bndbox>
<xmin>17.237</xmin>
<ymin>17.744</ymin>
<xmax>189.602</xmax>
<ymax>195.179</ymax>
<width>172.4</width>
<height>177.4</height>
</bndbox>
</object>
<object>
<name>RedArmor2</name>
<bndbox>
<xmin>140.173</xmin>
<ymin>128.007</ymin>
<xmax>173.125</xmax>
<ymax>152.087</ymax>
<width>33.0</width>
<height>24.1</height>
</bndbox>
</object>
<object>
<name>RedArmor2</name>
<bndbox>
<width>31.7</width>
<height>27.9</height>
</bndbox>
</object>
</annotation>

最新文章

  1. 【转】基于.NET平台常用的框架整理
  2. [COJ0528]BJOI幸运数
  3. HTML5自学笔记[ 21 ]canvas绘图实例之马赛克
  4. wp———图片切换效果
  5. Laravel 5.0 之命令及处理程序
  6. FIREDAC直连ORACLE数据库
  7. cocos2d-x 混合模式
  8. css伪类 伪元素
  9. String详细学习
  10. [09] 监听器 Listener
  11. strut2接收参数的三种方式
  12. [No0000B0]ReSharper操作指南1/16-入门与简介
  13. Extjs event domain 研究
  14. 对于java自定义的工具类的提炼 注意事项
  15. Java调用Lua脚本(热载实现)
  16. (转)JDK1.8新特性Lambda表达式
  17. JNI 数据类型转换
  18. cache的作用
  19. C语言 内存分配 地址 指针 数组 参数 实例解析
  20. Ubuntu使用yah3c连接校园网

热门文章

  1. uml类间关系总结
  2. drupal clean url 配置
  3. 浅谈JS输出中的“+”作用问题
  4. Vulnhub 靶机 CONTAINME: 1
  5. Oracle.DataAccess使用问题汇总
  6. 简单生成excel文件
  7. MySQL之校对集问题
  8. gym104076H
  9. docker实战(8)使用docker-compose快速搭建zookeeper集群
  10. python学习●错误点●expected an indented block