Python Forum

Full Version: reading content between a specific xml tag
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a spml file as below:

<?xml version="1.0" encoding="UTF-8"?>
<spml:batchresponse designation="software developers">

<response>



<position alias="role">Python developer</position>


</response>

<response>

<position alias="role">java developer</position>


</response>

</spml:batchresponse>
So ,My requirement is I should be able to check initially if designation=software developer.If it is software developer only i should extract the data between the <response></response> tag .I also want to access the data within <position alias="role"></position> tag

My code is right now:

import xml.etree.ElementTree as ET
tree = ET.parse('abc.spml')
root = tree.getroot()
for i in tree.findall('response'):
    print (ET.tostring(i))
But i am unable to access the data within the position alias="role" and i am unable to check if designation="software developers"

The above spml is just a sample spml file what i have.Any errors in the sample spml ,please overlook them
>>> data = '''<?xml version="1.0" encoding="UTF-8"?>
... <spml:batchresponse designation="software developers">
...  
... <response>
...  
...  
...  
... <position alias="role">Python developer</position>
...  
...  
... </response>
...  
... <response>
...  
... <position alias="role">java developer</position>
...  
...  
... </response>
...  
... </spml:batchresponse>'''

>>> from bs4 import BeautifulSoup

>>> soup = BeautifulSoup(data, 'xml')

>>> for tag in soup.findAll('position'):
...     print(tag.text)
...     
... 
Python developer
java developer