Top-rated recipes tagged "xml"http://code.activestate.com/recipes/tags/xml/top/2017-02-22T23:00:21-08:00ActiveState Code RecipesXML to Python data structure (Python)
2009-05-31T16:10:38-07:00Wai Yip Tunghttp://code.activestate.com/recipes/users/2382677/http://code.activestate.com/recipes/534109-xml-to-python-data-structure/
<p style="color: grey">
Python
recipe 534109
by <a href="/recipes/users/2382677/">Wai Yip Tung</a>
(<a href="/recipes/tags/xml/">xml</a>).
Revision 8.
</p>
<p>This simple method construct Python data structure from XML in one simple step. Data is accessed using the Pythonic "object.attribute" notation. See the discussion below for usage examples.</p>
Convert CSV to XML (Python)
2010-10-11T06:20:19-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577423-convert-csv-to-xml/
<p style="color: grey">
Python
recipe 577423
by <a href="/recipes/users/4172570/">FB36</a>
(<a href="/recipes/tags/csv/">csv</a>, <a href="/recipes/tags/xml/">xml</a>).
</p>
<p>Convert CSV to XML.</p>
xml to python dictionary and back (Python)
2012-10-24T00:44:30-07:00kris kvilekvalhttp://code.activestate.com/recipes/users/4178118/http://code.activestate.com/recipes/577722-xml-to-python-dictionary-and-back/
<p style="color: grey">
Python
recipe 577722
by <a href="/recipes/users/4178118/">kris kvilekval</a>
(<a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/mapping/">mapping</a>, <a href="/recipes/tags/xml/">xml</a>).
Revision 2.
</p>
<p>Simple mapping of XML to python dictionary based on Perl XML::Simple</p>
Nested contexts -- a chain of mapping objects (Python)
2010-10-25T02:13:37-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577434-nested-contexts-a-chain-of-mapping-objects/
<p style="color: grey">
Python
recipe 577434
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/ast/">ast</a>, <a href="/recipes/tags/chained/">chained</a>, <a href="/recipes/tags/compiler/">compiler</a>, <a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/inheritance/">inheritance</a>, <a href="/recipes/tags/nested/">nested</a>, <a href="/recipes/tags/nonlocal/">nonlocal</a>, <a href="/recipes/tags/scopes/">scopes</a>, <a href="/recipes/tags/xml/">xml</a>).
Revision 2.
</p>
<p>Easy to use chain of dictionaries for crafting nested scopes or for a tree of scopes. Useful for analyzing AST nodes, XML nodes or other structures with multiple scopes. Can emulate various chaining styles including static/lexical scoping, dynamic scoping and Python's own globals(), locals(), nested scopes, and writeable nonlocals. Can also model Python's inheritance chains: instance dictionary, class dictionary, and base classes.</p>
Converting XML to Dictionary and back (Python)
2009-01-15T11:42:18-08:00Cory Fabrehttp://code.activestate.com/recipes/users/4158087/http://code.activestate.com/recipes/573463-converting-xml-to-dictionary-and-back/
<p style="color: grey">
Python
recipe 573463
by <a href="/recipes/users/4158087/">Cory Fabre</a>
(<a href="/recipes/tags/xml/">xml</a>).
Revision 7.
</p>
<p>This code converts an XML ElementTree.Element into a dict and back into XML. It attempts to retain as much information as possible during the conversions while still being convenient to access.</p>
Validate XML with schemaLocation (Python)
2013-03-28T10:24:55-07:00Mathias Loeschhttp://code.activestate.com/recipes/users/4185910/http://code.activestate.com/recipes/578503-validate-xml-with-schemalocation/
<p style="color: grey">
Python
recipe 578503
by <a href="/recipes/users/4185910/">Mathias Loesch</a>
(<a href="/recipes/tags/schema/">schema</a>, <a href="/recipes/tags/schemalocation/">schemalocation</a>, <a href="/recipes/tags/validation/">validation</a>, <a href="/recipes/tags/xml/">xml</a>).
Revision 3.
</p>
<p>Addresses problems described at:</p>
<p><a href="http://stackoverflow.com/questions/2979824/in-document-schema-declarations-and-lxml" rel="nofollow">http://stackoverflow.com/questions/2979824/in-document-schema-declarations-and-lxml</a>
<a href="https://mailman-mail5.webfaction.com/pipermail/lxml/2011-September/006153.html" rel="nofollow">https://mailman-mail5.webfaction.com/pipermail/lxml/2011-September/006153.html</a></p>
Easy XML (Python)
2011-05-03T23:06:30-07:00Yony Kochinskihttp://code.activestate.com/recipes/users/4175703/http://code.activestate.com/recipes/577451-easy-xml/
<p style="color: grey">
Python
recipe 577451
by <a href="/recipes/users/4175703/">Yony Kochinski</a>
(<a href="/recipes/tags/xml/">xml</a>).
Revision 2.
</p>
<p>Being that I only use XML on occasion for some very basic features, I find myself having to re-learn the appropriate syntax every time. After doing this for the n-th time, I finally decided to wrap this syntax in something that's much more intuitive (to me, at least). The main goal is to make this as simple an API as possible, at the cost of some features.</p>
Converting XML to dict (for a XPATH-like access syntax) and back to XML (Python)
2007-06-28T03:16:28-07:00Rodrigo Strausshttp://code.activestate.com/recipes/users/2678660/http://code.activestate.com/recipes/522991-converting-xml-to-dict-for-a-xpath-like-access-syn/
<p style="color: grey">
Python
recipe 522991
by <a href="/recipes/users/2678660/">Rodrigo Strauss</a>
(<a href="/recipes/tags/xml/">xml</a>).
Revision 4.
</p>
<p>I really like config files done in Python language itself, using a dictionary declaration. It's cool for programmers, but not so cool for system administrators not used to Python (it's so easy to forget a comma...).
To keep using dictionaries internally providing something more admin friendly, I've done some functions to convert a XML file to Python dictionary (and the reverse as well):</p>
SAX to DOM Chunker (Python)
2004-08-06T08:48:59-07:00Uche Ogbujihttp://code.activestate.com/recipes/users/509776/http://code.activestate.com/recipes/298343-sax-to-dom-chunker/
<p style="color: grey">
Python
recipe 298343
by <a href="/recipes/users/509776/">Uche Ogbuji</a>
(<a href="/recipes/tags/xml/">xml</a>).
</p>
<p>This module is similar to pulldom in that it takes a stream of SAX objects and breaks it down into chunks of DOM. The differences are that it works with any DOM implementation meeting the Python DOM conventions, and that it uses simple pattern expressions to declaratively set how the DOM chunks are partitioned, rather than requiring the user to write procedural code for this purpose. This is an updated/fixed version of code that appeared in an <a href="http://XML.com" rel="nofollow">XML.com</a> column.</p>
Auto-detect XML encoding (Python)
2001-03-14T08:33:16-08:00Paul Prescodhttp://code.activestate.com/recipes/users/11203/http://code.activestate.com/recipes/52257-auto-detect-xml-encoding/
<p style="color: grey">
Python
recipe 52257
by <a href="/recipes/users/11203/">Paul Prescod</a>
(<a href="/recipes/tags/xml/">xml</a>).
</p>
<p>The XML specification describes the outlines of an algorithm for detecting the
Unicode encoding that an XML document uses. This function will do that.</p>
XML viewer for Tkinter or XML treeview (Python)
2017-02-22T23:00:21-08:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580752-xml-viewer-for-tkinter-or-xml-treeview/
<p style="color: grey">
Python
recipe 580752
by <a href="/recipes/users/4189907/">Miguel Martínez López</a>
(<a href="/recipes/tags/browser/">browser</a>, <a href="/recipes/tags/tkinter/">tkinter</a>, <a href="/recipes/tags/treeview/">treeview</a>, <a href="/recipes/tags/viewer/">viewer</a>, <a href="/recipes/tags/xml/">xml</a>).
Revision 3.
</p>
<p>Tk widget to display XML for Python. I use the treeview widget. There is no external dependency.I also added support for autoscrollbars.</p>
LXML Etree XML Object to basic Python {dict+lists} (Python)
2012-08-20T07:06:54-07:00Luis Martin Gilhttp://code.activestate.com/recipes/users/4183220/http://code.activestate.com/recipes/578244-lxml-etree-xml-object-to-basic-python-dictlists/
<p style="color: grey">
Python
recipe 578244
by <a href="/recipes/users/4183220/">Luis Martin Gil</a>
(<a href="/recipes/tags/lxml/">lxml</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/xml/">xml</a>).
</p>
<p>Convert an Etree XML structure into a Python dict+list</p>
XML as Dictionary (Python)
2012-01-17T18:10:29-08:00Vikram Baghelhttp://code.activestate.com/recipes/users/4180561/http://code.activestate.com/recipes/578017-xml-as-dictionary/
<p style="color: grey">
Python
recipe 578017
by <a href="/recipes/users/4180561/">Vikram Baghel</a>
(<a href="/recipes/tags/xml/">xml</a>).
</p>
<p>I use this for configuration. I hadn't intended to put it up anywhere, but there have been a couple discussions lately about converting XML to python dicts, so I feel obligated to share another approach, one that is based on Fredrik Lundh's ElementTree.</p>
dict2xml (Python)
2011-07-19T12:32:57-07:00nuggetierhttp://code.activestate.com/recipes/users/4178225/http://code.activestate.com/recipes/577739-dict2xml/
<p style="color: grey">
Python
recipe 577739
by <a href="/recipes/users/4178225/">nuggetier</a>
(<a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/xml/">xml</a>).
Revision 4.
</p>
<p>convert dictionary / list structures into xml structure</p>
python xml parsing (Python)
2011-05-28T19:36:00-07:00abhijeet vaidyahttp://code.activestate.com/recipes/users/4178141/http://code.activestate.com/recipes/577727-python-xml-parsing/
<p style="color: grey">
Python
recipe 577727
by <a href="/recipes/users/4178141/">abhijeet vaidya</a>
(<a href="/recipes/tags/parsing/">parsing</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/xml/">xml</a>).
</p>
<p>xml parsing to how to extract data</p>
Flatten XML to XPath syntax lines (Python)
2011-01-19T16:05:24-08:00Raphaël Jolivethttp://code.activestate.com/recipes/users/4135673/http://code.activestate.com/recipes/577547-flatten-xml-to-xpath-syntax-lines/
<p style="color: grey">
Python
recipe 577547
by <a href="/recipes/users/4135673/">Raphaël Jolivet</a>
(<a href="/recipes/tags/flatten/">flatten</a>, <a href="/recipes/tags/grep/">grep</a>, <a href="/recipes/tags/sed/">sed</a>, <a href="/recipes/tags/xml/">xml</a>, <a href="/recipes/tags/xpath/">xpath</a>).
</p>
<p>This script acts like <a href="http://www.ofb.net/~egnor/xml2/">xml2</a>.
It transforms a XML file into a flat text output, with <em>XPath</em>-like syntax, one line per XML node or attribute.
This format is more suitable for working with standard unix CLI utils (sed, grep, ... etc).</p>
Convert XML into JSON (Python dicts and lists structure) (Python)
2011-08-22T13:30:33-07:00Nikolahttp://code.activestate.com/recipes/users/4176190/http://code.activestate.com/recipes/577494-convert-xml-into-json-python-dicts-and-lists-struc/
<p style="color: grey">
Python
recipe 577494
by <a href="/recipes/users/4176190/">Nikola</a>
(<a href="/recipes/tags/converter/">converter</a>, <a href="/recipes/tags/json/">json</a>, <a href="/recipes/tags/xml/">xml</a>).
Revision 2.
</p>
<p>There are many ways to convert a specific XML dialect into JSON for easier consumption by Javascript code. This recipe works for some XML-based data, where the format is not really a document and html tags are used as field placeholders. You can use it as-is, but it is intended to serve as a starting point where you can plug your specific needs.</p>
Converting MySQL queries to XML (PHP)
2008-09-11T13:16:40-07:00sameer boratehttp://code.activestate.com/recipes/users/4167072/http://code.activestate.com/recipes/576499-converting-mysql-queries-to-xml/
<p style="color: grey">
PHP
recipe 576499
by <a href="/recipes/users/4167072/">sameer borate</a>
(<a href="/recipes/tags/mysql/">mysql</a>, <a href="/recipes/tags/php/">php</a>, <a href="/recipes/tags/xml/">xml</a>).
</p>
<p>Converting MySQL queries to XML</p>
XML to python dictionary of list (Python)
2008-04-16T15:17:30-07:00Vivek Khuranahttp://code.activestate.com/recipes/users/4125433/http://code.activestate.com/recipes/570085-xml-to-python-dictionary-of-list/
<p style="color: grey">
Python
recipe 570085
by <a href="/recipes/users/4125433/">Vivek Khurana</a>
(<a href="/recipes/tags/xml/">xml</a>).
Revision 4.
</p>
<p>Another recipe to convert xml file into a python dictionary. This recipe uses lxml</p>
Simple maps and lists from XML (Python)
2007-04-14T13:47:45-07:00Adam Hupphttp://code.activestate.com/recipes/users/117023/http://code.activestate.com/recipes/511472-simple-maps-and-lists-from-xml/
<p style="color: grey">
Python
recipe 511472
by <a href="/recipes/users/117023/">Adam Hupp</a>
(<a href="/recipes/tags/xml/">xml</a>).
</p>
<p>There are a several ways to represent collections of key/value pairs in XML, which makes it more difficult than necessary to use that data in python. By taking advantage of common patterns in the XML it is easy to turn most formats into collections of lists and dicts.</p>