xml containing 1 child
See original GitHub issueConsider 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:
- Created 11 years ago
- Reactions:3
- Comments:31 (8 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
How can I use the
force_list
option for ALL elements? Do I really have to list every element?What you could do is to nominate particular elements which should always generate lists, e.g.
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 notchildren
which is a list, butchild
which is a list.So to work with xmltodict, I think you need to specify the enclosing element as well:
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:
At the moment you get:
which loses the ordering relationship between the
foo
andbar
elements.This would have to be dealt with differently. For example:
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.