I have an XML file. I would like to retrieve the value of the tag. The return of the value is multiple line string. But my expectation the result is in array. Here is my XML file:
<StudentData>
<number no="130913" info="update">
<detail name="man1" class="A1">
<string name="john" />
<string name="piper" />
</detail>
<detail name="man2" class="A2">
<string name="steve" />
<string name="jobs" />
</detail>
</number>
</StudentData>
Here I got the value of the string name
.
ReadXML = ET.parse('test.xml')
for itm in ReadXML.findall('.//detail[@class="A1"]/'):
itemname = itm.get('name')
print(itemname)
The output of the code above is:
john
piper
But my expectation the output is become one list of array like this :
["john", "piper"]
I tried to split the output with this way:
print(itemname.splitlines())
But the output still like this:
['john']
['piper']
Anyone can help me and give me advice about this. I really appreciated it. Thank you very much.
Use a list comprehension:
print([itm.get("name") for itm in ReadXML.findall('.//detail[@class="A1"]/')])