0

Strange error occured, got a XML-file emailed to me which was wrongly formated. The info in the file was all in one row.

Like this

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Text><otherText><printdate>2015-02-08</printdate>

Does anyone know a quick way to fix this by using a python script or something that has had the same error?

I want to make the file like this.

<?xml version="1.0" encoding="ISO-8859-1"?>
<Text>
<OtherText>
<Name>VH2</Name>
<PrintDate>2015-02-05</PrintDate>

Thanks!

SterlinkArcher
  • 673
  • 3
  • 21
  • 36

2 Answers2

0

It seems you want to print pretty, if you look into other XML libraries, such as lxml, it support pretty print.

import lxml.etree as etree

x = etree.parse("filename")
print etree.tostring(x, pretty_print = True)

However, you can also try this: Pretty printing XML in Python

Community
  • 1
  • 1
Cui Heng
  • 1,265
  • 8
  • 10
0

If the XML is well formed this snippet will work

#!/usr/bin/python
import xml.dom.minidom

def main():
    ugly_xml   = open('ugly.xml', 'r')
    pretty_xml = open('pretty.xml', 'w')

    xmll = xml.dom.minidom.parseString(ugly_xml.read())
    pretty_xml.write(xmll.toprettyxml()) 

if __name__ == "__main__":
    main()
Jimmy Bernljung
  • 429
  • 2
  • 8