METADATA 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. Metadata-Version: 2.1
  2. Name: xmltodict
  3. Version: 0.13.0
  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.4
  16. Classifier: Programming Language :: Python :: 3.5
  17. Classifier: Programming Language :: Python :: 3.6
  18. Classifier: Programming Language :: Python :: 3.7
  19. Classifier: Programming Language :: Python :: 3.8
  20. Classifier: Programming Language :: Python :: 3.9
  21. Classifier: Programming Language :: Python :: 3.10
  22. Classifier: Programming Language :: Python :: Implementation :: PyPy
  23. Classifier: Topic :: Text Processing :: Markup :: XML
  24. Requires-Python: >=3.4
  25. Description-Content-Type: text/markdown
  26. License-File: LICENSE
  27. # xmltodict
  28. `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):
  29. [![Build Status](https://travis-ci.com/martinblech/xmltodict.svg?branch=master)](https://travis-ci.com/martinblech/xmltodict)
  30. ```python
  31. >>> print(json.dumps(xmltodict.parse("""
  32. ... <mydocument has="an attribute">
  33. ... <and>
  34. ... <many>elements</many>
  35. ... <many>more elements</many>
  36. ... </and>
  37. ... <plus a="complex">
  38. ... element as well
  39. ... </plus>
  40. ... </mydocument>
  41. ... """), indent=4))
  42. {
  43. "mydocument": {
  44. "@has": "an attribute",
  45. "and": {
  46. "many": [
  47. "elements",
  48. "more elements"
  49. ]
  50. },
  51. "plus": {
  52. "@a": "complex",
  53. "#text": "element as well"
  54. }
  55. }
  56. }
  57. ```
  58. ## Namespace support
  59. 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:
  60. ```python
  61. >>> xml = """
  62. ... <root xmlns="http://defaultns.com/"
  63. ... xmlns:a="http://a.com/"
  64. ... xmlns:b="http://b.com/">
  65. ... <x>1</x>
  66. ... <a:y>2</a:y>
  67. ... <b:z>3</b:z>
  68. ... </root>
  69. ... """
  70. >>> xmltodict.parse(xml, process_namespaces=True) == {
  71. ... 'http://defaultns.com/:root': {
  72. ... 'http://defaultns.com/:x': '1',
  73. ... 'http://a.com/:y': '2',
  74. ... 'http://b.com/:z': '3',
  75. ... }
  76. ... }
  77. True
  78. ```
  79. It also lets you collapse certain namespaces to shorthand prefixes, or skip them altogether:
  80. ```python
  81. >>> namespaces = {
  82. ... 'http://defaultns.com/': None, # skip this namespace
  83. ... 'http://a.com/': 'ns_a', # collapse "http://a.com/" -> "ns_a"
  84. ... }
  85. >>> xmltodict.parse(xml, process_namespaces=True, namespaces=namespaces) == {
  86. ... 'root': {
  87. ... 'x': '1',
  88. ... 'ns_a:y': '2',
  89. ... 'http://b.com/:z': '3',
  90. ... },
  91. ... }
  92. True
  93. ```
  94. ## Streaming mode
  95. `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/):
  96. ```python
  97. >>> def handle_artist(_, artist):
  98. ... print(artist['name'])
  99. ... return True
  100. >>>
  101. >>> xmltodict.parse(GzipFile('discogs_artists.xml.gz'),
  102. ... item_depth=2, item_callback=handle_artist)
  103. A Perfect Circle
  104. Fantômas
  105. King Crimson
  106. Chris Potter
  107. ...
  108. ```
  109. It can also be used from the command line to pipe objects to a script like this:
  110. ```python
  111. import sys, marshal
  112. while True:
  113. _, article = marshal.load(sys.stdin)
  114. print(article['title'])
  115. ```
  116. ```sh
  117. $ bunzip2 enwiki-pages-articles.xml.bz2 | xmltodict.py 2 | myscript.py
  118. AccessibleComputing
  119. Anarchism
  120. AfghanistanHistory
  121. AfghanistanGeography
  122. AfghanistanPeople
  123. AfghanistanCommunications
  124. Autism
  125. ...
  126. ```
  127. Or just cache the dicts so you don't have to parse that big XML file again. You do this only once:
  128. ```sh
  129. $ bunzip2 enwiki-pages-articles.xml.bz2 | xmltodict.py 2 | gzip > enwiki.dicts.gz
  130. ```
  131. And you reuse the dicts with every script that needs them:
  132. ```sh
  133. $ gunzip enwiki.dicts.gz | script1.py
  134. $ gunzip enwiki.dicts.gz | script2.py
  135. ...
  136. ```
  137. ## Roundtripping
  138. You can also convert in the other direction, using the `unparse()` method:
  139. ```python
  140. >>> mydict = {
  141. ... 'response': {
  142. ... 'status': 'good',
  143. ... 'last_updated': '2014-02-16T23:10:12Z',
  144. ... }
  145. ... }
  146. >>> print(unparse(mydict, pretty=True))
  147. <?xml version="1.0" encoding="utf-8"?>
  148. <response>
  149. <status>good</status>
  150. <last_updated>2014-02-16T23:10:12Z</last_updated>
  151. </response>
  152. ```
  153. 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`.
  154. ```python
  155. >>> import xmltodict
  156. >>>
  157. >>> mydict = {
  158. ... 'text': {
  159. ... '@color':'red',
  160. ... '@stroke':'2',
  161. ... '#text':'This is a test'
  162. ... }
  163. ... }
  164. >>> print(xmltodict.unparse(mydict, pretty=True))
  165. <?xml version="1.0" encoding="utf-8"?>
  166. <text stroke="2" color="red">This is a test</text>
  167. ```
  168. 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.
  169. ```python
  170. >>> mydict = {
  171. ... "line": {
  172. ... "points": [
  173. ... [1, 5],
  174. ... [2, 6],
  175. ... ]
  176. ... }
  177. ... }
  178. >>> print(xmltodict.unparse(mydict, pretty=True))
  179. <?xml version="1.0" encoding="utf-8"?>
  180. <line>
  181. <points>[1, 5]</points>
  182. <points>[2, 6]</points>
  183. </line>
  184. >>> print(xmltodict.unparse(mydict, pretty=True, expand_iter="coord"))
  185. <?xml version="1.0" encoding="utf-8"?>
  186. <line>
  187. <points>
  188. <coord>1</coord>
  189. <coord>5</coord>
  190. </points>
  191. <points>
  192. <coord>2</coord>
  193. <coord>6</coord>
  194. </points>
  195. </line>
  196. ```
  197. ## Ok, how do I get it?
  198. ### Using pypi
  199. You just need to
  200. ```sh
  201. $ pip install xmltodict
  202. ```
  203. ### RPM-based distro (Fedora, RHEL, …)
  204. There is an [official Fedora package for xmltodict](https://apps.fedoraproject.org/packages/python-xmltodict).
  205. ```sh
  206. $ sudo yum install python-xmltodict
  207. ```
  208. ### Arch Linux
  209. There is an [official Arch Linux package for xmltodict](https://www.archlinux.org/packages/community/any/python-xmltodict/).
  210. ```sh
  211. $ sudo pacman -S python-xmltodict
  212. ```
  213. ### Debian-based distro (Debian, Ubuntu, …)
  214. There is an [official Debian package for xmltodict](https://tracker.debian.org/pkg/python-xmltodict).
  215. ```sh
  216. $ sudo apt install python-xmltodict
  217. ```
  218. ### FreeBSD
  219. There is an [official FreeBSD port for xmltodict](https://svnweb.freebsd.org/ports/head/devel/py-xmltodict/).
  220. ```sh
  221. $ pkg install py36-xmltodict
  222. ```
  223. ### openSUSE/SLE (SLE 15, Leap 15, Tumbleweed)
  224. There is an [official openSUSE package for xmltodict](https://software.opensuse.org/package/python-xmltodict).
  225. ```sh
  226. # Python2
  227. $ zypper in python2-xmltodict
  228. # Python3
  229. $ zypper in python3-xmltodict
  230. ```