1

Is it possible to pass the text attribute of an subelement in the constructor? I'll describe. I want to join the second and third lines to one line:

import xml.etree.ElementTree as ET
son = ET.SubElement(parent, tagName) 
son.text = 'some string'

Thanks in advance

NI6
  • 2,477
  • 5
  • 17
  • 28

1 Answers1

3

I flagged this as a duplicate of How to set ElementTree Element text field in the constructor.

In short, no, the constructor doesn't support it. You can write a custom function though:

def text_element(parent, tag, text, *args, **kwargs):
    element = ET.SubElement(parent, tag, *args, **kwargs)
    element.text = text
    return element
Community
  • 1
  • 1
Jared Goguen
  • 8,772
  • 2
  • 18
  • 36