xmltodict
xmltodict copied to clipboard
XML or text declaration not at start of entity
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'?>
.....
"""
<?xml version="1.0" encoding="Windows-1251" standalone="yes"?>
<a></a>
may be converted to
{
"a": {
},
"#encoding": "Windows-1251",
"#standalone": "yes"
}
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 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