简体   繁体   中英

Error when parsing XML document using xmltodict python

I've copied over an XML document below, which is titled 'dictionary.xml' For some reason, the document cannot be parsed by XmlToDict in python.

<dictmap>

<lexelt item="begin.v" num="4">
<sense id="1" wordnet="2,3,5,6,7" gloss="start, have an initial point " examples="Prices for these homes begin at $250,000 | The war began on a Thursday. | She began to feel sick. | When life began was there oxygen in the atmosphere? | The novel begins with a shipwreck at sea. | It's beginning to look like rain. | His property begins at the fence." />
<sense id="2" wordnet="1,8,5" gloss="initiate an undertaking" examples="They'll begin their tour with a concert in London. | You should begin the stew now so it will have four hours to simmer. | The government is beginning to protect the coral reefs of that island. | He was just beginning a novel when the phone rang. | Acme Tire Co. began offering their employees health insurance. | John has begun to take them seriously. | She began ballet at age four." />
<sense id="3" wordnet="4" gloss="make a locution, speak" examples=""now listen, friends", he began." />
<sense id="4" wordnet="9" gloss="partially attain" examples="The rent you could get for that place wouldn't begin to cover the mortgage and taxes. | Can those refugees even begin to hope they'll be granted asylum? | I couldn't begin to tell you all the ways she has contributed to this club." />
</lexelt>

<lexelt item="complain.v" num="2">
<sense id="1" wordnet="1" gloss="express feelings of pain, dissatisfaction, or resentment" examples="Lots of people have complained about the noise. | You're always complaining! | He complained that his boss was useless and he had too much work. | She's complaining about a headache." />
<sense id="2" wordnet="2" gloss="make formal, legal accusation or bring charge" examples="The neighbors complained to the police about the noise. | We have complained to the authorities and they will need to take action now. | The lawyer complained that the defendant had physically abused his client." />
</lexelt>


</dictmap>

The following code should work but is triggering the following error:

xmltodict.parse('dictionary.xml')

>>>>
---------------------------------------------------------------------------
ExpatError                                Traceback (most recent call last)
<ipython-input-16-c098f9f722f0> in <module>
----> 1 xmltodict.parse('dictionary.xml')

~/opt/anaconda3/lib/python3.7/site-packages/xmltodict.py in parse(xml_input, encoding, expat, process_namespaces, namespace_separator, disable_entities, **kwargs)
    325         parser.ParseFile(xml_input)
    326     else:
--> 327         parser.Parse(xml_input, True)
    328     return handler.item
    329 

ExpatError: syntax error: line 1, column 0

What do I need to do to correct the issue?

Change the quotes from

examples=""now listen, friends", he began."

to

examples="&quot;now listen, friends&quot;, he began."

as explained here: How to escape double quotes in XML attributes values?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM