I have a source section in an XML from which I am trying to fetch the values in this way to a text file.
source1, ipset-1, IPSet, true
source2, ipset-2, IPSet, true
XML section:
<sources excluded="false">
<source>
<name>source1</name>
<value>ipset-1</value>
<type>IPSet</type>
<isValid>true</isValid>
</source>
<source>
<name>source2</name>
<value>ipset-2</value>
<type>IPSet</type>
<isValid>true</isValid>
</source>
</sources>
Currently, my code gives me everything in one line.
import xml.etree.ElementTree as ET
tree = ET.fromstring(xml_file)
for node in tree.iter('source'):
print('\n')
with open("source.txt", "a") as file:
for elem in node.iter():
if not elem.tag==node.tag:
file.write("{},".format(elem.text))
print("{}: {}".format(elem.tag, elem.text))