Tags
external tools, format, gedit, Linux, python, xml, xml formatting
Have you ever got an XML that is in a messed up format and had a hard time in trying to figure out what is there? And felt that, what if there is a tool that you can use to format it…
If you are using GEdit as your editor, here is a solution for you:
- Install python-lxml (Or look for lxml)
- Create a new external tool configuration (Tools -> Manage External Tools…) and add a new command. Like : (Format XML)
- This is the script you need to execute:
#! /usr/bin/env python
import sys
import lxml.etree as etree
import traceback
result = ”
for line in sys.stdin:
result += line
try:
x = etree.fromstring(result)
result = etree.tostring(x, pretty_print=True)
except:
etype, evalue, etraceback = sys.exc_info()
traceback.print_exception(etype, evalue, etraceback, file=sys.stderr)
print result
- Set Input to Current document
- Set Output to Replace current document
Once done, you will have a menu option under Tools -> External Tools -> Format XML. Once you click on it, you can enjoy understanding XML easily 🙂 Obviously, being a software engineer, I reused the above given code from somewhere else 😀