Python flask cannot open an xml file
Question: I try to implement a server-side multilanguage service on my website. This is
the structure on the folders:

    
    
    data
    --locale
    static
    --css
    --images
    --js
    templates
    --index.html
    --page1.html
    ...
    main.py
    

I use Crowdin to translate the website and the output files are in XML. The
locale folder contains one folder for each language with one xml file for
every page.

I store on Cookies the language and here is my python code:

    
    
    from flask import request
    from xml.dom.minidom import parseString
    
    def languages(page):
        langcode = request.cookies.get("Language")
        xml = "/data/locale/%s/%s.xml" % (langcode, page)
    
        dom = parseString(xml)
        ................
        .............
    

Which I call in every page, like `languages("index")`

This is an example of the exported xml files

    
    
    <?xml version="1.0" encoding="utf-8"?>
    <!--Generated by crowdin.com-->
    <!--
    This is a description of my page
    -->
    <resources>
      <string name="name1">value 1</string>
      <string name="name2">value 2</string>
      <string name="name3">value 3</string>
    </resources>
    

However, I have the following error `ExpatError: not well-formed (invalid
token): line 1, column 0`

I googled it. I ended up to other stackoverflow questions, but most of them
says about encoding problems and I cannot find any in my example.


Answer: You have to use `parse()` if you want to parse a file. `parseString()` will
parse a string, the file name in your case.

    
    
    from flask import request
    from xml.dom.minidom import parse
    
    def languages(page):
        langcode = request.cookies.get("Language")
        xml = "/data/locale/%s/%s.xml" % (langcode, page)
    
        dom = parse(xml)
    

