Fix xml.parsers.expat.ExpatError: XML or text declaration not at start of entity Error – Python Tutorial

By | September 16, 2020

We can use python xml.dom.minidom package to parse a xml file.

Here is an example:

Python Parse XML Sitemap to Extract Urls: A Simple Guide – Python Tutorial

However, you may get this error: xml.parsers.expat.ExpatError: XML or text declaration not at start of entity. In this tutorial, we will introduce you how to fix this error.

fix xml.parsers.expat.ExpatError - XML or text declaration not at start of entity

As to our code, the full error is:

xml.parsers.expat.ExpatError: XML or text declaration not at start of entity: line 2, column 0

We can find the error occur in line 2 and column 0.

How to fix this error?

Open xml file and look at the line 2.

the reason of xml.parsers.expat.ExpatError - XML or text declaration not at start of entity

We can find there is an empty line in this file.

Remove this empty line.

Run code again, we will find this error is fixed.

Leave a Reply