1

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?

Volatil3
  • 14,253
  • 38
  • 134
  • 263

1 Answers1

1

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'})
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • hmm interesting. It screwed up everything else: `books = document['books]['book']` not working anymore; gives error `list indices must be integers, not str` – Volatil3 Apr 08 '16 at 05:02
  • @Volatil3 yeah, now it always is a list: `[book['author'] for book in document['books']]`..but I actually find it more logical.. – alecxe Apr 08 '16 at 05:05
  • I should have put entire XML. I just updated the Qs, there are not iterative elements as well and from what I am seeing, it seems it ruined the entire format – Volatil3 Apr 08 '16 at 05:13
  • https://github.com/martinblech/xmltodict/issues/14#issuecomment-143434259 should work for me but it's now call `force_list` an invalid keyword – Volatil3 Apr 08 '16 at 05:13
  • 1
    OK I install latest by doing `sudo pip install git+https://github.com/martinblech/xmltodict.git@master ` and it worked like charm – Volatil3 Apr 08 '16 at 05:27
  • @Volatil3 okay, had to update accordingly, thanks for sharing. – alecxe Apr 08 '16 at 05:38