Search code examples
pythonpython-2.7xmltodict

xmltodict: Not fetching correct nested elements


So I have an XML like that:

<main>
   <site>Amazon</site>
   <url>..</url>
    <books>
    <book>
        <id>1</id>
        <author>Jhon</author>
    </book>
    <book>
        <id>2</id>
        <author>Jhon</author>
    </book>
    <book>
        <id>3</id>
        <author>Jhon</author>
    </book>
    </books>
</main>

I am accessing it as:

document = xmltodict.parse(xml)
books = document['books]['book'] #returns 3
for book in books:
   pass

But if XML is like:

<books>
 <book>
    <id>3</id>
    <author>Jhon</author>
 </book>
</books>

then instead of 1 it returns 2 that is child of <books>

What am I doing wrong?


Solution

  • This is a common problem with xmltodict, which was discussed here:

    The workaround to this behavior would be to use a force_list option (available in the master branch at the moment):

    xmltodict.parse(data, force_list={'books': 'book'})