i'm having trouble creating the xml file in python. My script at start loads a text file, read each line and check if the content contains a specific string, depending on it i want to create a tag or in the xml file.
For that i'm using the ElementTree module.
This the code
for line in myfile.read().splitlines():
if 'EXTINF' in line:
root = etree.Element("item")
etree.SubElement(root, "title").text = line
elif 'http' in line:
etree.SubElement(root, "link").text = 'http:\\mysite.com\'
tree = etree.ElementTree(root)
tree.write('my\\path\\'+xml_file.xml)
The xml file contains only the last element iterated by for loop.
The output i would like is:
<item>
<title> "my title" </title>
<link> "http:\\mysite.com" </link>
</item>
<item>
<title> "my title" </title>
<link> "http:\\mysite.com" </link>
</item>
<item>
<title> "my title" </title>
<link> "http:\\mysite.com" </link>
</item>
What's wrong? Thanks