Most viewed recipes tagged "xml"http://code.activestate.com/recipes/tags/xml/views/2011-01-19T16:05:24-08:00ActiveState Code RecipesConvert 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 as Dictionary (Python) 2005-05-08T17:58:54-07:00Duncan McGreggorhttp://code.activestate.com/recipes/users/1923213/http://code.activestate.com/recipes/410469-xml-as-dictionary/ <p style="color: grey"> Python recipe 410469 by <a href="/recipes/users/1923213/">Duncan McGreggor</a> (<a href="/recipes/tags/xml/">xml</a>). Revision 5. </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> XML 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> 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> Simple XML serlializer/de-serializer using Python dictionaries and marshalling (Python) 2005-06-07T09:13:40-07:00Anandhttp://code.activestate.com/recipes/users/760763/http://code.activestate.com/recipes/415983-simple-xml-serlializerde-serializer-using-python-d/ <p style="color: grey"> Python recipe 415983 by <a href="/recipes/users/760763/">Anand</a> (<a href="/recipes/tags/xml/">xml</a>). Revision 2. </p> <p>This recipe presents a way of serializing &amp; de-serializing XML using the marshal module. The XML is converted to an equivalent Python dictionary first, which is marshaled to serialize it. De-serialization first unmarshals the dictionary from the file, and constructs the original XML.</p> Python data structure to XML (serialization) (Python) 2010-06-16T20:26:21-07:00David McCuskeyhttp://code.activestate.com/recipes/users/4174198/http://code.activestate.com/recipes/577268-python-data-structure-to-xml-serialization/ <p style="color: grey"> Python recipe 577268 by <a href="/recipes/users/4174198/">David McCuskey</a> (<a href="/recipes/tags/serialize/">serialize</a>, <a href="/recipes/tags/xml/">xml</a>). Revision 2. </p> <p>This code serializes Python data structure into XML.</p> <p>This is one part of a trio of recipes:</p> <ul> <li><a href="http://code.activestate.com/recipes/577266">Simple API</a></li> <li><a href="http://code.activestate.com/recipes/577267">XML2Py De-serialization</a></li> <li><a href="http://code.activestate.com/recipes/577268">Py2XML Serialization</a></li> </ul> <h5>For more information</h5> <p>See <a href="http://code.activestate.com/recipes/577266">XML to Python data structure <a href="http://code.activestate.com/recipes/577266/">Recipe #577266</a></a></p> Check xml well-formedness (Python) 2001-06-14T19:41:15-07:00Paul Prescodhttp://code.activestate.com/recipes/users/11203/http://code.activestate.com/recipes/52256-check-xml-well-formedness/ <p style="color: grey"> Python recipe 52256 by <a href="/recipes/users/11203/">Paul Prescod</a> (<a href="/recipes/tags/xml/">xml</a>). Revision 2. </p> <p>This small script will check whether one or more XML documents are well-formed.</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> Pretty-print XML (Python) 2009-05-13T07:47:45-07:00Anandhttp://code.activestate.com/recipes/users/760763/http://code.activestate.com/recipes/576750-pretty-print-xml/ <p style="color: grey"> Python recipe 576750 by <a href="/recipes/users/760763/">Anand</a> (<a href="/recipes/tags/text/">text</a>, <a href="/recipes/tags/xml/">xml</a>). </p> <p>One-liner to pretty print an XML file using xml.dom.minidom.</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> XML to Python data structure (De-/Serialization) (Python) 2010-06-16T23:29:34-07:00David McCuskeyhttp://code.activestate.com/recipes/users/4174198/http://code.activestate.com/recipes/577266-xml-to-python-data-structure-de-serialization/ <p style="color: grey"> Python recipe 577266 by <a href="/recipes/users/4174198/">David McCuskey</a> (<a href="/recipes/tags/deserialize/">deserialize</a>, <a href="/recipes/tags/serialize/">serialize</a>, <a href="/recipes/tags/xml/">xml</a>). Revision 2. </p> <p>This is a simple interface to a collection of classes which serialize Python objects to XML and back.</p> <p>This is one part of a trio of recipes:</p> <ul> <li><a href="http://code.activestate.com/recipes/577266">Simple API</a></li> <li><a href="http://code.activestate.com/recipes/577267">XML2Py De-serialization</a></li> <li><a href="http://code.activestate.com/recipes/577268">Py2XML Serialization</a></li> </ul> Parsing Excel XML (Python) 2003-04-01T03:33:22-08:00Thomas Guettlerhttp://code.activestate.com/recipes/users/1077859/http://code.activestate.com/recipes/192914-parsing-excel-xml/ <p style="color: grey"> Python recipe 192914 by <a href="/recipes/users/1077859/">Thomas Guettler</a> (<a href="/recipes/tags/xml/">xml</a>). </p> <p>This script parses an MS-Excel spreadsheet saved as XML.</p> <p>The spreadsheet: a1, b1 a2, b2</p> <p>will be stored like this:</p> <p>[[u'a1', u'b1'], [u'a2', u'b2']]</p> Validating XML with External DTDs using xmlproc (Python) 2003-09-08T18:34:20-07:00Paul Sholtzhttp://code.activestate.com/recipes/users/1358430/http://code.activestate.com/recipes/220472-validating-xml-with-external-dtds-using-xmlproc/ <p style="color: grey"> Python recipe 220472 by <a href="/recipes/users/1358430/">Paul Sholtz</a> (<a href="/recipes/tags/xml/">xml</a>). </p> <p>PyXML is a useful package for parsing XML. The xmlval and xmldtd modules let you validate XML docs against an external DTD file. This is a simple, straightforward recipe that illustrates how to use the xmlval and xmldtd modules for validated XML parsing.</p> ElementTree CDATA support (Python) 2008-10-10T12:29:33-07:00Eli Golovinskyhttp://code.activestate.com/recipes/users/2770692/http://code.activestate.com/recipes/576536-elementtree-cdata-support/ <p style="color: grey"> Python recipe 576536 by <a href="/recipes/users/2770692/">Eli Golovinsky</a> (<a href="/recipes/tags/elementtree/">elementtree</a>, <a href="/recipes/tags/xml/">xml</a>). Revision 2. </p> <p>This recipe monkey-patches the ElementTree library to allow correct parsing and generation of CDATA sections.</p> Parsing an XML file with xml.parsers.expat (Python) 2001-07-27T15:31:46-07:00Mark Nenadovhttp://code.activestate.com/recipes/users/114221/http://code.activestate.com/recipes/65248-parsing-an-xml-file-with-xmlparsersexpat/ <p style="color: grey"> Python recipe 65248 by <a href="/recipes/users/114221/">Mark Nenadov</a> (<a href="/recipes/tags/xml/">xml</a>). Revision 4. </p> <p>This is a reusable way to use "xml.parsers.expat" to parse an XML file. When re-using the "MyXML" class, all you need to define a new class, with "MyXML" as the parent. Once you have done that, all you have to do is overwrite the inherited XML handlers and you are ready to go.</p> Detect character encoding in an XML file (Python) 2005-01-20T00:50:37-08:00Lars Tiedehttp://code.activestate.com/recipes/users/2270421/http://code.activestate.com/recipes/363841-detect-character-encoding-in-an-xml-file/ <p style="color: grey"> Python recipe 363841 by <a href="/recipes/users/2270421/">Lars Tiede</a> (<a href="/recipes/tags/xml/">xml</a>). Revision 2. </p> <p>A function analyzing an open xml file for its character encoding by - checking for a unicode BOM or (on failure) - searching the xml declaration at the beginning of the file for the "encoding" attribute</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> 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> Extract text from XML document (Python) 2001-06-14T19:42:23-07:00Paul Prescodhttp://code.activestate.com/recipes/users/11203/http://code.activestate.com/recipes/65128-extract-text-from-xml-document/ <p style="color: grey"> Python recipe 65128 by <a href="/recipes/users/11203/">Paul Prescod</a> (<a href="/recipes/tags/xml/">xml</a>). Revision 2. </p> <p>People often ask how to extract the text from an XML document. This small program does it.</p> Convert Wordpress Export File to multiple html files (Python) 2008-03-24T04:57:36-07:00Luiss Reihttp://code.activestate.com/recipes/users/4136205/http://code.activestate.com/recipes/551792-convert-wordpress-export-file-to-multiple-html-fil/ <p style="color: grey"> Python recipe 551792 by <a href="/recipes/users/4136205/">Luiss Rei</a> (<a href="/recipes/tags/xml/">xml</a>). </p> <p>Converts Wordpress Export Files (XML) to multiple html files and optionally uses tags and authors to create a directory structure.</p>