格式化XML输出(移除版本声明,过滤空行)
(self, elem: ET.Element)
| 444 | return mxfile |
| 445 | |
| 446 | def _prettify_xml(self, elem: ET.Element) -> str: |
| 447 | """格式化XML输出(移除版本声明,过滤空行)""" |
| 448 | rough_string = ET.tostring(elem, "utf-8") |
| 449 | reparsed = minidom.parseString(rough_string) |
| 450 | |
| 451 | lines = reparsed.toprettyxml(indent=" ").split('\n') |
| 452 | return '\n'.join([ |
| 453 | line for line in lines |
| 454 | if line.strip() and not line.strip().startswith("<?xml") |
| 455 | ]) |
| 456 | |
| 457 | # ======================== 便捷方法 ======================== |
| 458 |