Home > Blockchain >  May I know how to create / generate XML using elementtree
May I know how to create / generate XML using elementtree

Time:10-11

...
para1= ET.SubElement(root,'para')
anchortag=ET.SubElement(para1,'anchor')

anchortag.set()
para1.text= " sometexthere"

I tried with the above code snippet,but couldn't get the expected output.I don't want to create a new para tag that would take the text to newline. result of above code : <para> sometexthere<anchor> xyxyy</anchor></para>

Expected code

<para><anchor> xyxyy</anchor> sometexthere </para>

CodePudding user response:

You have to make a couple of changes, and it should work:

First, change

anchortag.set()

to

anchortag.text='xyxy'

and then, given that you want sometexthere to be at the tail end of the element,

change

para1.text= " sometexthere"

to

para1.tail= "sometexthere"
  • Related