-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
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
xml.etree.ElementTree: add feature to prettify XML output #58670
Comments
I often miss lxml's "pretty_print=True" functionality. Can you implement something similar. |
Would you like to provide a patch? |
Tshepang, Frankly, there are a lot of issues to solve in ElementTree (it hasn't been given love in a long time...) and such features would be low priority, as I'm not getting much help and am swamped already. As Martin said, patches can go a long way here... |
Okay, I will try, even though C scares me. |
You may be able to code it entirely in the Python part of the module (adding a new parameter to Element.write and tostring). |
A patch exists in the duplicate bpo-17372 |
Proposed patch copied over from duplicate bpo-17372. |
Just to reiterate this point, lxml.etree supports a "pretty_print" flag in its tostring() function and ElementTree.write(). It would thus make sense to support the same thing in ET. http://lxml.de/api.html#serialisation For completeness, the current signature looks like this: def tostring(element_or_tree, *, encoding=None, method="xml",
xml_declaration=None, pretty_print=False,
with_tail=True, standalone=None, doctype=None,
exclusive=False, with_comments=True,
inclusive_ns_prefixes=None): (The last three options are for C14N serialisation.) |
For the record, at 2015-04-02, the bpo-23847 has been marked as a duplicate of this issue. |
My thoughts:
https://www.w3.org/TR/xml/#sec-white-space
|
Should I say it? That's a first class use case for Cython.
Agreed. And that would actually be much simpler to implement in C. |
A few more thoughts for consideration:
|
I have a use case where the receiving application is expecting the indentation, and I need to run my code in Lambda. So, lxml is out of the question. |
FWIW, here is the relevant section of the XML specification, https://www.w3.org/TR/2008/REC-xml-20081126/#sec-white-space : """In editing XML documents, it is often convenient to use "white space" (spaces, tabs, and blank lines) to set apart the markup for greater readability. Such white space is typically not intended for inclusion in the delivered version of the document. On the other hand, "significant" white space that should be preserved in the delivered version is common, for example in poetry and source code. An XML processor must always pass all characters in a document that are not markup through to the application. A validating XML processor must also inform the application which of these characters constitute white space appearing in element content. OTOH, the java TransformerFactory does support a property, OutputKeys.INDENT, so there is a precedent for this feature request. Stefan, would you please make a final determination or pronouncement on whether this makes sense for ElementTree or whether it is outside the scope of what the module is trying to accomplish. |
The spec section that Raymond quoted makes it clear that pretty printing is not for everyone. But there are many use cases where it is 1) helpful, 2) leads to correct results, and 3) does not grow the file size excessively. Whoever wants to make use of it is probably in such a situation. I think adding some kind of support in the standard library would be nice, but it should not hurt "normal" uses, especially when a lot of data is involved. I'll send a PR that adds an indent() function to pre-process trees. Comments welcome. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: