Questions tagged [xmltodict]

Python module that makes working with XML feel like you are working with JSON.

xmltodict is a Python module that makes working with XML feel like you are working with JSON

Check it on Pypi here.

120 questions
28
votes
4 answers

xmltodict does not return a list for one element

The following Code produces an error, if there is only one "car" in "garage": import xmltodict mydict = xmltodict.parse(xmlstringResults) for carsInGarage in mydict['garage']['car']: # do something... The Reason is that…
user2111880
  • 671
  • 9
  • 17
21
votes
4 answers

How can I use xmltodict to get items out of an XML file?

I am trying to easily access values from an XML file like this: ABC123 Highlight pen 231 0.56
54m
  • 719
  • 2
  • 7
  • 18
12
votes
2 answers

Catch ExpatError in xmltodict

I am using xmltodict to parse xml. If we parse invalid xml, it throws up an ExpatError. How do I catch this? Here is what I've tried in my ipython shell >>> import xmltodict >>> xml_data = """ ... …
Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
9
votes
1 answer

Handle 1 to n elements

I'm using xmltodict to parse an XML config. The XML has structures where an element can occur in 1 to n instances, where both are valid: abc and abc
balas
  • 216
  • 3
  • 11
6
votes
4 answers

ImportError: No module named 'xmltodict'

Relatively new to Python I've created a project using Pipenv that uses the xmltodict module. I've looked at some similar questions but can't find exactly what I need to do. I've used pipenv to install the xmltodict module but I'm still getting the…
MattB
  • 135
  • 1
  • 3
  • 10
5
votes
1 answer

Python xmltodict: How to preserve XML element order?

I'm using xmltodict for XML parsing/unparsing, and I need to preserve the XML element ordering while processing one document. Toy REPL example: >>> import xmltodict >>> xml = """ ... ... ... ... ... ... """ >>>…
el.atomo
  • 5,200
  • 3
  • 30
  • 28
5
votes
2 answers

TypeError: list indices must be integers, not str with xmltodict:

I have got this XML file:
user977828
  • 7,259
  • 16
  • 66
  • 117
5
votes
3 answers

Parsing an xml file with an ordered dictionary

I have an xml file of the form: and nd 0 8262
Omid
  • 2,617
  • 4
  • 28
  • 43
4
votes
2 answers

Flask Api to parse xml post requests returning errors

I am writing a python-flask api to parse and manipulate xml using xml to dict. Below is my initial code: from flask import Flask, jsonify, abort, make_response, request, render_template import sys, sqlite3, requests, datetime, time, re, json,…
Daniel
  • 169
  • 1
  • 4
  • 10
4
votes
2 answers

xmltodict.unparse is not handling CDATA properly

I am trying to use xmltodict to manipulate an XML content as python object, but I am facing an issue to handle properly CDATA. I think I am missing something somewhere, this is my code: import xmltodict data = """
hzrari
  • 1,803
  • 1
  • 15
  • 26
4
votes
2 answers

ValueError with xmltodict unparse() function - Python 3

I'm having trouble using xmltodict to convert json to xml. It works fine with a single root and a single object, however when I try to convert multiple objects it returns a ValueError "ValueError: document with multiple roots". Here's my JSON…
lagunazul
  • 257
  • 3
  • 5
  • 10
3
votes
1 answer

Stream large XML file directly from GridFS to xmltodict parsing

I am using Motor for async MongoDB operations. I have a gridfs storage where I store large XML files (typically 30+ MB in size) in chunks of 8 MBs. I want to incrementally parse the XML file using xmltodict. Here is how my code looks. async def…
Shiladitya Bose
  • 893
  • 2
  • 13
  • 31
3
votes
0 answers

How to use xmltodict.parse with non-printable data

I tried to convert xml to dict with Python import xmltodict xmltodict.parse('as\x08') But I got an exception ExpatError Traceback (most recent call last) in ----> 1…
python3.789
  • 164
  • 9
3
votes
1 answer

Generate consistent JSON from XML for sometimes repeated elements with xmltodict

For converting XML to JSON with python, several methods exist which usually boil down to the same principle. The most referred method today is xmltodict. I'm trying to convert multiple xml documents to JSON, but run into issues if elements that are…
Jeroen
  • 452
  • 5
  • 12
3
votes
3 answers

How to convert an xml to a dictionary with some modifications?

I currently have an xml file in the following format: 97 98 99
Laura Smith
  • 293
  • 3
  • 13
1
2 3 4 5 6 7 8