xmltodict icon indicating copy to clipboard operation
xmltodict copied to clipboard

XML or text declaration not at start of entity

Open Doskulov opened this issue 6 years ago • 3 comments

how to parse with xml main tag, if i will remove it everything works fine, but i'm integrating with other service, so i need to parse with this: data = """ <?xml version='1.0' encoding='Windows-1251' standalone='yes'?> ..... """

Doskulov avatar Jul 20 '18 13:07 Doskulov

<?xml version="1.0" encoding="Windows-1251" standalone="yes"?>
<a></a>

may be converted to

{
  "a": {
  },
  "#encoding": "Windows-1251",
  "#standalone": "yes"
}

javadev avatar Nov 21 '18 05:11 javadev

Looks like the exception text is referring to the new line at the start of the xml.

Fails:

r = xmltodict.parse('''
... <?xml version="1.0" encoding="Windows-1251" standalone="yes"?>
... <a></a>
... ''')
Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
  File "/home/ubuntu/environment/mdda-service/artefacts/record-ingestion/venv/lib/python3.7/site-packages/xmltodict.py", line 327, in parse
    parser.Parse(xml_input, True)
xml.parsers.expat.ExpatError: XML or text declaration not at start of entity: line 2, column 0

Works (no whitespace):

r = xmltodict.parse(
... '''<?xml version="1.0" encoding="Windows-1251" standalone="yes"?>
... <a></a>
... ''')
>>> r
OrderedDict([('a', None)])

Works (no xml declaration):

r = xmltodict.parse('''
... <a></a>
... ''')
>>> r
OrderedDict([('a', None)])

AlexHilson avatar Jan 14 '20 18:01 AlexHilson

@AlexHilson is it possible to parse an XML having a newline at the beginning? I am getting the XML response from a server, and there is nothing I can do to remove that line from the server-side. I can do some file manipulation at the client-side, but I expect the library to perform that task.

I can even contribute in developing if this can be an added feature

sashank27 avatar Dec 19 '20 11:12 sashank27