Parsing an arbitrary XML file with ElementTree
--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Lost Meadow
--
Chapters
00:00 Parsing An Arbitrary Xml File With Elementtree
01:56 Answer 1 Score 0
02:10 Accepted Answer Score 4
02:37 Thank you
--
Full question
https://stackoverflow.com/questions/1551...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #xml #elementtree
#avk47
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Lost Meadow
--
Chapters
00:00 Parsing An Arbitrary Xml File With Elementtree
01:56 Answer 1 Score 0
02:10 Accepted Answer Score 4
02:37 Thank you
--
Full question
https://stackoverflow.com/questions/1551...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #xml #elementtree
#avk47
ACCEPTED ANSWER
Score 4
Using ElementTree:
import xml.etree.ElementTree as et
filehandler = open("file.xml","r")
raw_data = et.parse(filehandler)
data_root = raw_data.getroot()
filehandler.close()
for children in data_root:
for child in children:
print(child.tag, child.text, children.tag, children.text)
That will give you an overview of the XML-tags and associated text inside tags. You can add more loops to step further into the tree, and perform checks to see wether any of the children contains further levels. I find this method useful when the name of the XML tags varies and does not follow an already known standard.
ANSWER 2
Score 0
An example using BeautifulSoup:
import sys
from bs4 import BeautifulSoup
file = sys.argv[1]
handler = open(file).read()
soup = BeautifulSoup(handler)
for table in soup.find_all("target_table"):
for loc in table.find_all("rep"):
print loc.xlocation.string + ", " + loc.ylocation.string
Output
nextXREL, nextYREL