METADATA 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. Metadata-Version: 2.1
  2. Name: xmltodict
  3. Version: 0.14.2
  4. Summary: Makes working with XML feel like you are working with JSON
  5. Home-page: https://github.com/martinblech/xmltodict
  6. Author: Martin Blech
  7. Author-email: martinblech@gmail.com
  8. License: MIT
  9. Platform: all
  10. Classifier: Intended Audience :: Developers
  11. Classifier: License :: OSI Approved :: MIT License
  12. Classifier: Operating System :: OS Independent
  13. Classifier: Programming Language :: Python
  14. Classifier: Programming Language :: Python :: 3
  15. Classifier: Programming Language :: Python :: 3.6
  16. Classifier: Programming Language :: Python :: 3.7
  17. Classifier: Programming Language :: Python :: 3.8
  18. Classifier: Programming Language :: Python :: 3.9
  19. Classifier: Programming Language :: Python :: 3.10
  20. Classifier: Programming Language :: Python :: 3.11
  21. Classifier: Programming Language :: Python :: 3.12
  22. Classifier: Programming Language :: Python :: 3.13
  23. Classifier: Programming Language :: Python :: Implementation :: PyPy
  24. Classifier: Topic :: Text Processing :: Markup :: XML
  25. Requires-Python: >=3.6
  26. Description-Content-Type: text/markdown
  27. License-File: LICENSE
  28. # xmltodict
  29. `xmltodict` is a Python module that makes working with XML feel like you are working with [JSON](http://docs.python.org/library/json.html), as in this ["spec"](http://www.xml.com/pub/a/2006/05/31/converting-between-xml-and-json.html):
  30. [![Build Status](https://app.travis-ci.com/martinblech/xmltodict.svg?branch=master)](https://app.travis-ci.com/martinblech/xmltodict)
  31. ```python
  32. >>> print(json.dumps(xmltodict.parse("""
  33. ... <mydocument has="an attribute">
  34. ... <and>
  35. ... <many>elements</many>
  36. ... <many>more elements</many>
  37. ... </and>
  38. ... <plus a="complex">
  39. ... element as well
  40. ... </plus>
  41. ... </mydocument>
  42. ... """), indent=4))
  43. {
  44. "mydocument": {
  45. "@has": "an attribute",
  46. "and": {
  47. "many": [
  48. "elements",
  49. "more elements"
  50. ]
  51. },
  52. "plus": {
  53. "@a": "complex",
  54. "#text": "element as well"
  55. }
  56. }
  57. }
  58. ```
  59. ## Namespace support
  60. By default, `xmltodict` does no XML namespace processing (it just treats namespace declarations as regular node attributes), but passing `process_namespaces=True` will make it expand namespaces for you:
  61. ```python
  62. >>> xml = """
  63. ... <root xmlns="http://defaultns.com/"
  64. ... xmlns:a="http://a.com/"
  65. ... xmlns:b="http://b.com/">
  66. ... <x>1</x>
  67. ... <a:y>2</a:y>
  68. ... <b:z>3</b:z>
  69. ... </root>
  70. ... """
  71. >>> xmltodict.parse(xml, process_namespaces=True) == {
  72. ... 'http://defaultns.com/:root': {
  73. ... 'http://defaultns.com/:x': '1',
  74. ... 'http://a.com/:y': '2',
  75. ... 'http://b.com/:z': '3',
  76. ... }
  77. ... }
  78. True
  79. ```
  80. It also lets you collapse certain namespaces to shorthand prefixes, or skip them altogether:
  81. ```python
  82. >>> namespaces = {
  83. ... 'http://defaultns.com/': None, # skip this namespace
  84. ... 'http://a.com/': 'ns_a', # collapse "http://a.com/" -> "ns_a"
  85. ... }
  86. >>> xmltodict.parse(xml, process_namespaces=True, namespaces=namespaces) == {
  87. ... 'root': {
  88. ... 'x': '1',
  89. ... 'ns_a:y': '2',
  90. ... 'http://b.com/:z': '3',
  91. ... },
  92. ... }
  93. True
  94. ```
  95. ## Streaming mode
  96. `xmltodict` is very fast ([Expat](http://docs.python.org/library/pyexpat.html)-based) and has a streaming mode with a small memory footprint, suitable for big XML dumps like [Discogs](http://discogs.com/data/) or [Wikipedia](http://dumps.wikimedia.org/):
  97. ```python
  98. >>> def handle_artist(_, artist):
  99. ... print(artist['name'])
  100. ... return True
  101. >>>
  102. >>> xmltodict.parse(GzipFile('discogs_artists.xml.gz'),
  103. ... item_depth=2, item_callback=handle_artist)
  104. A Perfect Circle
  105. Fantômas
  106. King Crimson
  107. Chris Potter
  108. ...
  109. ```
  110. It can also be used from the command line to pipe objects to a script like this:
  111. ```python
  112. import sys, marshal
  113. while True:
  114. _, article = marshal.load(sys.stdin)
  115. print(article['title'])
  116. ```
  117. ```sh
  118. $ bunzip2 enwiki-pages-articles.xml.bz2 | xmltodict.py 2 | myscript.py
  119. AccessibleComputing
  120. Anarchism
  121. AfghanistanHistory
  122. AfghanistanGeography
  123. AfghanistanPeople
  124. AfghanistanCommunications
  125. Autism
  126. ...
  127. ```
  128. Or just cache the dicts so you don't have to parse that big XML file again. You do this only once:
  129. ```sh
  130. $ bunzip2 enwiki-pages-articles.xml.bz2 | xmltodict.py 2 | gzip > enwiki.dicts.gz
  131. ```
  132. And you reuse the dicts with every script that needs them:
  133. ```sh
  134. $ gunzip enwiki.dicts.gz | script1.py
  135. $ gunzip enwiki.dicts.gz | script2.py
  136. ...
  137. ```
  138. ## Roundtripping
  139. You can also convert in the other direction, using the `unparse()` method:
  140. ```python
  141. >>> mydict = {
  142. ... 'response': {
  143. ... 'status': 'good',
  144. ... 'last_updated': '2014-02-16T23:10:12Z',
  145. ... }
  146. ... }
  147. >>> print(unparse(mydict, pretty=True))
  148. <?xml version="1.0" encoding="utf-8"?>
  149. <response>
  150. <status>good</status>
  151. <last_updated>2014-02-16T23:10:12Z</last_updated>
  152. </response>
  153. ```
  154. Text values for nodes can be specified with the `cdata_key` key in the python dict, while node properties can be specified with the `attr_prefix` prefixed to the key name in the python dict. The default value for `attr_prefix` is `@` and the default value for `cdata_key` is `#text`.
  155. ```python
  156. >>> import xmltodict
  157. >>>
  158. >>> mydict = {
  159. ... 'text': {
  160. ... '@color':'red',
  161. ... '@stroke':'2',
  162. ... '#text':'This is a test'
  163. ... }
  164. ... }
  165. >>> print(xmltodict.unparse(mydict, pretty=True))
  166. <?xml version="1.0" encoding="utf-8"?>
  167. <text stroke="2" color="red">This is a test</text>
  168. ```
  169. Lists that are specified under a key in a dictionary use the key as a tag for each item. But if a list does have a parent key, for example if a list exists inside another list, it does not have a tag to use and the items are converted to a string as shown in the example below. To give tags to nested lists, use the `expand_iter` keyword argument to provide a tag as demonstrated below. Note that using `expand_iter` will break roundtripping.
  170. ```python
  171. >>> mydict = {
  172. ... "line": {
  173. ... "points": [
  174. ... [1, 5],
  175. ... [2, 6],
  176. ... ]
  177. ... }
  178. ... }
  179. >>> print(xmltodict.unparse(mydict, pretty=True))
  180. <?xml version="1.0" encoding="utf-8"?>
  181. <line>
  182. <points>[1, 5]</points>
  183. <points>[2, 6]</points>
  184. </line>
  185. >>> print(xmltodict.unparse(mydict, pretty=True, expand_iter="coord"))
  186. <?xml version="1.0" encoding="utf-8"?>
  187. <line>
  188. <points>
  189. <coord>1</coord>
  190. <coord>5</coord>
  191. </points>
  192. <points>
  193. <coord>2</coord>
  194. <coord>6</coord>
  195. </points>
  196. </line>
  197. ```
  198. ## Ok, how do I get it?
  199. ### Using pypi
  200. You just need to
  201. ```sh
  202. $ pip install xmltodict
  203. ```
  204. ### Using conda
  205. For installing `xmltodict` using Anaconda/Miniconda (*conda*) from the
  206. [conda-forge channel][#xmltodict-conda] all you need to do is:
  207. [#xmltodict-conda]: https://anaconda.org/conda-forge/xmltodict
  208. ```sh
  209. $ conda install -c conda-forge xmltodict
  210. ```
  211. ### RPM-based distro (Fedora, RHEL, …)
  212. There is an [official Fedora package for xmltodict](https://apps.fedoraproject.org/packages/python-xmltodict).
  213. ```sh
  214. $ sudo yum install python-xmltodict
  215. ```
  216. ### Arch Linux
  217. There is an [official Arch Linux package for xmltodict](https://www.archlinux.org/packages/community/any/python-xmltodict/).
  218. ```sh
  219. $ sudo pacman -S python-xmltodict
  220. ```
  221. ### Debian-based distro (Debian, Ubuntu, …)
  222. There is an [official Debian package for xmltodict](https://tracker.debian.org/pkg/python-xmltodict).
  223. ```sh
  224. $ sudo apt install python-xmltodict
  225. ```
  226. ### FreeBSD
  227. There is an [official FreeBSD port for xmltodict](https://svnweb.freebsd.org/ports/head/devel/py-xmltodict/).
  228. ```sh
  229. $ pkg install py36-xmltodict
  230. ```
  231. ### openSUSE/SLE (SLE 15, Leap 15, Tumbleweed)
  232. There is an [official openSUSE package for xmltodict](https://software.opensuse.org/package/python-xmltodict).
  233. ```sh
  234. # Python2
  235. $ zypper in python2-xmltodict
  236. # Python3
  237. $ zypper in python3-xmltodict
  238. ```