question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

xml containing 1 child

See original GitHub issue

Consider the following code

xml = """<?xml version="1.0" encoding="utf-8" ?>
<root>
    <children>
        <child>
            <name>A</name>
        </child>
    </children>
</root>"""

xmltodict.parse(xml)['root']['children']['child']

Wouldn’t you expect to have an iterable object even when there is only 1 child?

Issue Analytics

  • State:closed
  • Created 11 years ago
  • Reactions:3
  • Comments:31 (8 by maintainers)

github_iconTop GitHub Comments

9reactions
acjacksoncommented, Feb 2, 2017

How can I use the force_list option for ALL elements? Do I really have to list every element?

7reactions
candlerbcommented, Sep 26, 2015

What you could do is to nominate particular elements which should always generate lists, e.g.

xmltodict.parse(xml, force_list=set('child'))

This is the approach taken by Perl’s XML::Simple with the ForceArray setting.

A problem with this is that if the <children> element is empty, it’s not going to give you an empty list of {"child": []}. This is due to how xmltodict groups elements at a lower level, i.e. it’s not children which is a list, but child which is a list.

So to work with xmltodict, I think you need to specify the enclosing element as well:

xmltodict.parse(xml, force_list={'children': 'child'})

Implies: “if you see a <children> element then always build a <child> with empty list underneath it”. Any actual <child> elements will then be added to that list.

Aside: there is a related but different problem, which is dealing with a heterogenous sequence of repeating elements. For example:

<stuff>
  <foo>.1.</foo>
  <bar>.2.</bar>
  <bar>.3.</bar>
  <foo>.4.</foo>
  <bar>.5.</bar>
</stuff>

At the moment you get:

OrderedDict([(u'stuff', OrderedDict([(u'foo', [u'.1.', u'.4.']), (u'bar', [u'.2.', u'.3.', u'.5.'])]))])

which loses the ordering relationship between the foo and bar elements.

This would have to be dealt with differently. For example:

xmltodict.parse(xml, force_list={'stuff': True})

could mean that <stuff> is forced to have a list of children, and that this list should contain repeated elements rather than grouped elements. I don’t know how practical that is to implement though. And it would probably have implications for unparse.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can a XML element contain text and child ... - Stack Overflow
Yes. A parent node contains zero or more child nodes. Text nodes and element nodes are two kinds of nodes and an element...
Read more >
XML Child nodes by name - Microsoft Q&A
XML Child nodes by name. Working on my non existent C# XML skills . ... Does just like I want for the time...
Read more >
SimpleXMLElement::children - Manual - PHP
Returns a SimpleXMLElement element, whether the node has children or not, unless the node represents an attribute, in which case null is returned....
Read more >
A Roadmap to XML Parsers in Python - Real Python
In this tutorial, you'll learn what XML parsers are available in Python and how to pick the right parsing model for your specific...
Read more >
xml.dom — The Document Object Model API — Python 3.11.1 ...
Return a new Document object (the root of the DOM), with a child Element object having the given namespaceUri and qualifiedName. The doctype...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found