How to parse a user-uploaded XML file in Python Dash?

New to Python and Dash, so sorry if this is a simple question.
Looked online but could not find any answers.

I have a dcc.Upload object with an html.Button, where users can upload an XML file.
Then, using a callback function, I would like to parse this file and return some simple data from the XML.
In regular python I would do this with

tree = et.parse("file.xml")
root = tree.getroot()
...
for node in root:
    title = node.find("series_title").....

but this does not seem to work in Dash/HTML.
Any idea how I would go about parsing files uploaded on Dash?
I think it has something to with the ‘contents’ data created when uploading a file, but a bit lost.

Any help is appreciated, thank you!

Figured it out, and will leave this here in case others come across the same issue.

Looks like the ‘contents’ of the file is read in as ‘content_string’ in base64.
Decode it first, then set up a tree, which you can then get the nodes in each root from.

content_type, content_string = contents.split(',')
    # content_string is in base64, so decode it
    decoded = base64.b64decode(content_string)
    tree = et.ElementTree(et.fromstring(decoded))

    root = tree.getroot()
    for node in root:....
1 Like