[Answer]-Python xml processing – how to loop through all elements?

1πŸ‘

βœ…

Your sample code works for me perfectly with Python 2.7.4 and lxml 3.2.3, the xpath() method returning a list containing the two Restaurant elements. If your XML parser insists that the XML file is well-formed, maybe you are giving it the wrong file, or the file is saved in a non-recognized encoding such as UTF-16.

To debug this, try printing open(settings.MEDIA_ROOT+'\\table.xml').read() and see if the output looks like valid XML. If in doubt about the correctness of XML, it’s a good idea to also verify it using an independent tool such as xmllint.

0πŸ‘

I got it by doing so:

datei = ET.parse(settings.MEDIA_ROOT+'\\table.xml')
    rests = datei.getroot()
    for each in rests:
      #do something with tags

thanks anyway for answers and views..

πŸ‘€doniyor

Leave a comment