Python Forum
reading content between a specific xml tag
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
reading content between a specific xml tag
#1
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
Reply
#2
>>> 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
rob101 and saisankalpj like this post
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading Specific Rows In a CSV File finndude 3 1,002 Dec-13-2022, 03:19 PM
Last Post: finndude
  Extracting Specific Lines from text file based on content. jokerfmj 8 3,044 Mar-28-2022, 03:38 PM
Last Post: snippsat
  Reading specific rows from CVS to Excel DavidTheGrockle 3 2,686 Nov-06-2019, 04:49 PM
Last Post: DavidTheGrockle
  Delete specific lines contain specific words mannyi 2 4,149 Nov-04-2019, 04:50 PM
Last Post: mannyi
  Reading specific rows (lookup) rumbles 3 3,373 Jan-03-2018, 04:07 PM
Last Post: hshivaraj

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020