[New-bugs-announce] [issue33561] Add .tostring() method to xml.etree.ElementTree.Element

Steven Vascellaro report at bugs.python.org
Thu May 17 13:13:45 EDT 2018


New submission from Steven Vascellaro <Stevoisiak at gmail.com>:

In Python 3.6, converting an xml `xml.etree.ElementTree.Element` to a string is done using `xml.etree.ElementTree.tostring()`.

```
from xml.etree import ElementTree

xml = ElementTree.Element('Person', Name='John')
print(ElementTree.tostring(xml, encoding='unicode', method='xml')
# Output: <Person Name="John" />
```

I would like to propose adding a `tostring()` function to the `Element` class, so that `ElementTree.tostring(xml)` could be replaced with the more intuitive `xml.tostring()`.

```
from xml.etree import ElementTree

xml = ElementTree.Element('Person', Name='John')
print(xml.tostring(encoding='unicode', method='xml'))
# Output: <Person Name="John" />
```

Benefits:

- Doesn't require importing `xml.etree.ElementTree`
- Allows writing more concise code
- Makes `tostring` part of the `Element` class
- Maintains backwards compatibility

----------
components: XML
messages: 316966
nosy: Stevoisiak
priority: normal
severity: normal
status: open
title: Add .tostring() method to xml.etree.ElementTree.Element
type: enhancement
versions: Python 3.6

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue33561>
_______________________________________


More information about the New-bugs-announce mailing list