METADATA 6.2 KB

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