Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't use regex to clean up XML #150

Closed
alxndrsn opened this issue Aug 21, 2017 · 1 comment · Fixed by #681
Closed

Don't use regex to clean up XML #150

alxndrsn opened this issue Aug 21, 2017 · 1 comment · Fixed by #681

Comments

@alxndrsn
Copy link
Contributor

alxndrsn commented Aug 21, 2017

Survey._to_pretty_xml pretty-prints XML output using a combination of minidom and regular expressions.

minidom formats the XML a bit, but also introduces some strange whitespace, which the regular expressions then attempt to clean up. While this is reasonably successful, it's not perfect. Simpler would be to use a different XML formatter to minidom which actually works out of the box.

The in-line TODO suggests checking out PyXML as per http://ronrothman.com/public/leftbraned/xml-dom-minidom-toprettyxml-and-silly-whitespace/#best-solution:

from xml.dom.ext import PrettyPrint
from StringIO import StringIO

def toprettyxml_fixed (node, encoding='utf-8'):
    tmpStream = StringIO()
    PrettyPrint(node, stream=tmpStream, encoding=encoding)
    return tmpStream.getvalue()
@batkinson
Copy link
Contributor

One thing to think about (and test) when changing to a different library is whether its dependencies are able to be embedded in downstream applications successfully.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants