XML Pretty Printing
Bill Scherer
Bill.Scherer at VerizonWireless.com
Thu Oct 19 11:32:55 EDT 2000
Paul Johnston wrote:
>
> Has anyone seen or created a method to print XML docs with all appropriate
> whitespace and indentation?
>
> Paul
>
> --
> http://www.python.org/mailman/listinfo/python-list
The code below outputs 'pretty' xml, not beautiful ;-). It requires
pyxie (www.pyxie.org).
#!/usr/bin/env python
'''xmlpprint hack'''
import sys, pyxie
xml = sys.stdin.read()
pyx = pyxie.String2PYX(xml)
pad = 0
for line in pyx.readlines():
chr1 = line[0]
if chr1 == '(':
pad = pad + 1
print ' ' * pad, '<' + line[1:-1] + '>'
continue
elif chr1 == ')':
pad = pad - 1
print (' ' * pad)[1:], '</' + line[1:-1] + '>'
continue
print ' ' * pad, line[1:],
#-------------------------------------------------------------
--
William K. Scherer
Sr. Member of Applications Staff - Verizon Wireless
Bill.Scherer_at_VerizonWireless.com
More information about the Python-list
mailing list