user1138880
user1138880

Reputation: 221

Malformed XML repair using python

I have 50 XML files which has mismatched tags and I want to repair them using python. The opening tag <names> is different from closing tag </name>. Can anyone guide me please.

    <breakfast_menu>
      <food>
        <names>Belgian Waffles</name>
        <price>$5.95</price>
        <calories>650</calories>
     </food>
    </breakfast_menu>

Upvotes: 4

Views: 4026

Answers (1)

TerryA
TerryA

Reputation: 60004

BeautifulSoup does this:

>>> from bs4 import BeautifulSoup
>>> myxml = # Your posted XML
>>> soup = BeautifulSoup(myxml,'xml')
>>> print soup
<?xml version="1.0" encoding="utf-8"?>
<breakfast_menu>
<food>
<names>Belgian Waffles</names>
<price>$5.95</price>
<calories>650</calories>
</food>
</breakfast_menu>

If you were looking for <name></name>:

>>> for i in soup.findAll('names'):
...     i.name = 'name'
...
>>> print soup
<?xml version="1.0" encoding="utf-8"?>
<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<calories>650</calories>
</food>
</breakfast_menu>

Upvotes: 6

Related Questions