<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>XMLWriter — Astropy v0.2.4</title> <link rel="stylesheet" href="../_static/bootstrap-astropy.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '0.2.4', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/sidebar.js"></script> <link rel="shortcut icon" href="../_static/astropy_logo.ico"/> <link rel="top" title="Astropy v0.2.4" href="../index.html" /> <link rel="up" title="Astropy Core Package Utilities (astropy.utils)" href="../utils/index.html" /> <link rel="next" title="Current status of sub-packages" href="../stability.html" /> <link rel="prev" title="xml_escape_cdata" href="astropy.utils.xml.writer.xml_escape_cdata.html" /> </head> <body> <div class="topbar"> <a class="brand" title="Documentation Home" href="../index.html"></a> <ul> <li><a class="homelink" title="AstroPy Homepage" href="http://www.astropy.org"></a></li> <li><a title="General Index" href="../genindex.html">Index</a></li> <li><a title="Python Module Index" href="../py-modindex.html">Modules</a></li> <li> <form action="../search.html" method="get"> <input type="text" name="q" placeholder="Search" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </li> </ul> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right"> <a href="../stability.html" title="Current status of sub-packages"> next » </a> </li> <li class="right"> <a href="astropy.utils.xml.writer.xml_escape_cdata.html" title="xml_escape_cdata"> « previous </a> | </li> <li> <a href="../index.html">Astropy v0.2.4</a> » </li> <li><a href="../utils/index.html" accesskey="U">Astropy Core Package Utilities (<tt class="docutils literal"><span class="pre">astropy.utils</span></tt>)</a> »</li> <li>XMLWriter</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="xmlwriter"> <h1>XMLWriter<a class="headerlink" href="#xmlwriter" title="Permalink to this headline">¶</a></h1> <dl class="class"> <dt id="astropy.utils.xml.writer.XMLWriter"> <em class="property">class </em><tt class="descclassname">astropy.utils.xml.writer.</tt><tt class="descname">XMLWriter</tt><big>(</big><em>file</em><big>)</big><a class="reference external" href="http://github.com/astropy/astropy/tree/v0.2.4/astropy/utils/xml/writer.py#L34" title="Push the Edit button on the next page"><span class="edit-on-github viewcode-link"> [edit on github]</span></a><a class="reference internal" href="../_modules/astropy/utils/xml/writer.html#XMLWriter"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#astropy.utils.xml.writer.XMLWriter" title="Permalink to this definition">¶</a></dt> <dd><p>A class to write well-formed and nicely indented XML.</p> <p>Use like this:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">w</span> <span class="o">=</span> <span class="n">XMLWriter</span><span class="p">(</span><span class="n">fh</span><span class="p">)</span> <span class="k">with</span> <span class="n">w</span><span class="o">.</span><span class="n">tag</span><span class="p">(</span><span class="s">'html'</span><span class="p">):</span> <span class="k">with</span> <span class="n">w</span><span class="o">.</span><span class="n">tag</span><span class="p">(</span><span class="s">'body'</span><span class="p">):</span> <span class="n">w</span><span class="o">.</span><span class="n">data</span><span class="p">(</span><span class="s">'This is the content'</span><span class="p">)</span> </pre></div> </div> <p>Which produces:</p> <div class="highlight-python"><pre><html> <body> This is the content </body> </html></pre> </div> <p class="rubric">Methods Summary</p> <table border="1" class="longtable docutils"> <colgroup> <col width="10%" /> <col width="90%" /> </colgroup> <tbody valign="top"> <tr class="row-odd"><td><a class="reference internal" href="#astropy.utils.xml.writer.XMLWriter.close" title="astropy.utils.xml.writer.XMLWriter.close"><tt class="xref py py-obj docutils literal"><span class="pre">close</span></tt></a>(id)</td> <td>Closes open elements, up to (and including) the element identified by the given identifier.</td> </tr> <tr class="row-even"><td><a class="reference internal" href="#astropy.utils.xml.writer.XMLWriter.comment" title="astropy.utils.xml.writer.XMLWriter.comment"><tt class="xref py py-obj docutils literal"><span class="pre">comment</span></tt></a>(comment)</td> <td>Adds a comment to the output stream.</td> </tr> <tr class="row-odd"><td><a class="reference internal" href="#astropy.utils.xml.writer.XMLWriter.data" title="astropy.utils.xml.writer.XMLWriter.data"><tt class="xref py py-obj docutils literal"><span class="pre">data</span></tt></a>(text)</td> <td>Adds character data to the output stream.</td> </tr> <tr class="row-even"><td><a class="reference internal" href="#astropy.utils.xml.writer.XMLWriter.element" title="astropy.utils.xml.writer.XMLWriter.element"><tt class="xref py py-obj docutils literal"><span class="pre">element</span></tt></a>(tag[, text, wrap, attrib])</td> <td>Adds an entire element.</td> </tr> <tr class="row-odd"><td><a class="reference internal" href="#astropy.utils.xml.writer.XMLWriter.end" title="astropy.utils.xml.writer.XMLWriter.end"><tt class="xref py py-obj docutils literal"><span class="pre">end</span></tt></a>([tag, indent, wrap])</td> <td>Closes the current element (opened by the most recent call to <a class="reference internal" href="#astropy.utils.xml.writer.XMLWriter.start" title="astropy.utils.xml.writer.XMLWriter.start"><tt class="xref py py-obj docutils literal"><span class="pre">start</span></tt></a>).</td> </tr> <tr class="row-even"><td><a class="reference internal" href="#astropy.utils.xml.writer.XMLWriter.flush" title="astropy.utils.xml.writer.XMLWriter.flush"><tt class="xref py py-obj docutils literal"><span class="pre">flush</span></tt></a>()</td> <td></td> </tr> <tr class="row-odd"><td><a class="reference internal" href="#astropy.utils.xml.writer.XMLWriter.get_indentation" title="astropy.utils.xml.writer.XMLWriter.get_indentation"><tt class="xref py py-obj docutils literal"><span class="pre">get_indentation</span></tt></a>()</td> <td>Returns the number of indentation levels the file is currently in.</td> </tr> <tr class="row-even"><td><a class="reference internal" href="#astropy.utils.xml.writer.XMLWriter.get_indentation_spaces" title="astropy.utils.xml.writer.XMLWriter.get_indentation_spaces"><tt class="xref py py-obj docutils literal"><span class="pre">get_indentation_spaces</span></tt></a>([offset])</td> <td>Returns a string of spaces that matches the current indentation level.</td> </tr> <tr class="row-odd"><td><a class="reference internal" href="#astropy.utils.xml.writer.XMLWriter.object_attrs" title="astropy.utils.xml.writer.XMLWriter.object_attrs"><tt class="xref py py-obj docutils literal"><span class="pre">object_attrs</span></tt></a>(obj, attrs)</td> <td>Converts an object with a bunch of attributes on an object into a dictionary for use by the <a class="reference internal" href="#astropy.utils.xml.writer.XMLWriter" title="astropy.utils.xml.writer.XMLWriter"><tt class="xref py py-obj docutils literal"><span class="pre">XMLWriter</span></tt></a>.</td> </tr> <tr class="row-even"><td><a class="reference internal" href="#astropy.utils.xml.writer.XMLWriter.start" title="astropy.utils.xml.writer.XMLWriter.start"><tt class="xref py py-obj docutils literal"><span class="pre">start</span></tt></a>(tag[, attrib])</td> <td>Opens a new element.</td> </tr> <tr class="row-odd"><td><a class="reference internal" href="#astropy.utils.xml.writer.XMLWriter.tag" title="astropy.utils.xml.writer.XMLWriter.tag"><tt class="xref py py-obj docutils literal"><span class="pre">tag</span></tt></a>(*args, **kwds)</td> <td>A convenience method for use with the <tt class="xref py py-obj docutils literal"><span class="pre">with</span></tt> statement:: with writer.tag(‘foo’): writer.element(‘bar’) # </foo> is implicitly closed here Parameters are the same as to <a class="reference internal" href="#astropy.utils.xml.writer.XMLWriter.start" title="astropy.utils.xml.writer.XMLWriter.start"><tt class="xref py py-obj docutils literal"><span class="pre">start</span></tt></a>.</td> </tr> </tbody> </table> <p class="rubric">Methods Documentation</p> <dl class="method"> <dt id="astropy.utils.xml.writer.XMLWriter.close"> <tt class="descname">close</tt><big>(</big><em>id</em><big>)</big><a class="reference external" href="http://github.com/astropy/astropy/tree/v0.2.4/astropy/utils/xml/writer.py#L213" title="Push the Edit button on the next page"><span class="edit-on-github viewcode-link"> [edit on github]</span></a><a class="reference internal" href="../_modules/astropy/utils/xml/writer.html#XMLWriter.close"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#astropy.utils.xml.writer.XMLWriter.close" title="Permalink to this definition">¶</a></dt> <dd><p>Closes open elements, up to (and including) the element identified by the given identifier.</p> <table class="docutils field-list" frame="void" rules="none"> <col class="field-name" /> <col class="field-body" /> <tbody valign="top"> <tr class="field-odd field"><th class="field-name">Parameters :</th><td class="field-body"><p class="first"><strong>id</strong> : int</p> <blockquote class="last"> <div><p>Element identifier, as returned by the <a class="reference internal" href="#astropy.utils.xml.writer.XMLWriter.start" title="astropy.utils.xml.writer.XMLWriter.start"><tt class="xref py py-obj docutils literal"><span class="pre">start</span></tt></a> method.</p> </div></blockquote> </td> </tr> </tbody> </table> </dd></dl> <dl class="method"> <dt id="astropy.utils.xml.writer.XMLWriter.comment"> <tt class="descname">comment</tt><big>(</big><em>comment</em><big>)</big><a class="reference external" href="http://github.com/astropy/astropy/tree/v0.2.4/astropy/utils/xml/writer.py#L161" title="Push the Edit button on the next page"><span class="edit-on-github viewcode-link"> [edit on github]</span></a><a class="reference internal" href="../_modules/astropy/utils/xml/writer.html#XMLWriter.comment"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#astropy.utils.xml.writer.XMLWriter.comment" title="Permalink to this definition">¶</a></dt> <dd><p>Adds a comment to the output stream.</p> <table class="docutils field-list" frame="void" rules="none"> <col class="field-name" /> <col class="field-body" /> <tbody valign="top"> <tr class="field-odd field"><th class="field-name">Parameters :</th><td class="field-body"><p class="first"><strong>comment</strong> : str</p> <blockquote class="last"> <div><p>Comment text, as a Unicode string.</p> </div></blockquote> </td> </tr> </tbody> </table> </dd></dl> <dl class="method"> <dt id="astropy.utils.xml.writer.XMLWriter.data"> <tt class="descname">data</tt><big>(</big><em>text</em><big>)</big><a class="reference external" href="http://github.com/astropy/astropy/tree/v0.2.4/astropy/utils/xml/writer.py#L174" title="Push the Edit button on the next page"><span class="edit-on-github viewcode-link"> [edit on github]</span></a><a class="reference internal" href="../_modules/astropy/utils/xml/writer.html#XMLWriter.data"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#astropy.utils.xml.writer.XMLWriter.data" title="Permalink to this definition">¶</a></dt> <dd><p>Adds character data to the output stream.</p> <table class="docutils field-list" frame="void" rules="none"> <col class="field-name" /> <col class="field-body" /> <tbody valign="top"> <tr class="field-odd field"><th class="field-name">Parameters :</th><td class="field-body"><p class="first"><strong>text</strong> : str</p> <blockquote class="last"> <div><p>Character data, as a Unicode string.</p> </div></blockquote> </td> </tr> </tbody> </table> </dd></dl> <dl class="method"> <dt id="astropy.utils.xml.writer.XMLWriter.element"> <tt class="descname">element</tt><big>(</big><em>tag</em>, <em>text=None</em>, <em>wrap=False</em>, <em>attrib={}</em>, <em>**extra</em><big>)</big><a class="reference external" href="http://github.com/astropy/astropy/tree/v0.2.4/astropy/utils/xml/writer.py#L226" title="Push the Edit button on the next page"><span class="edit-on-github viewcode-link"> [edit on github]</span></a><a class="reference internal" href="../_modules/astropy/utils/xml/writer.html#XMLWriter.element"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#astropy.utils.xml.writer.XMLWriter.element" title="Permalink to this definition">¶</a></dt> <dd><p>Adds an entire element. This is the same as calling <a class="reference internal" href="#astropy.utils.xml.writer.XMLWriter.start" title="astropy.utils.xml.writer.XMLWriter.start"><tt class="xref py py-obj docutils literal"><span class="pre">start</span></tt></a>, <a class="reference internal" href="#astropy.utils.xml.writer.XMLWriter.data" title="astropy.utils.xml.writer.XMLWriter.data"><tt class="xref py py-obj docutils literal"><span class="pre">data</span></tt></a>, and <a class="reference internal" href="#astropy.utils.xml.writer.XMLWriter.end" title="astropy.utils.xml.writer.XMLWriter.end"><tt class="xref py py-obj docutils literal"><span class="pre">end</span></tt></a> in sequence. The <tt class="xref py py-obj docutils literal"><span class="pre">text</span></tt> argument can be omitted.</p> </dd></dl> <dl class="method"> <dt id="astropy.utils.xml.writer.XMLWriter.end"> <tt class="descname">end</tt><big>(</big><em>tag=None</em>, <em>indent=True</em>, <em>wrap=False</em><big>)</big><a class="reference external" href="http://github.com/astropy/astropy/tree/v0.2.4/astropy/utils/xml/writer.py#L185" title="Push the Edit button on the next page"><span class="edit-on-github viewcode-link"> [edit on github]</span></a><a class="reference internal" href="../_modules/astropy/utils/xml/writer.html#XMLWriter.end"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#astropy.utils.xml.writer.XMLWriter.end" title="Permalink to this definition">¶</a></dt> <dd><p>Closes the current element (opened by the most recent call to <a class="reference internal" href="#astropy.utils.xml.writer.XMLWriter.start" title="astropy.utils.xml.writer.XMLWriter.start"><tt class="xref py py-obj docutils literal"><span class="pre">start</span></tt></a>).</p> <table class="docutils field-list" frame="void" rules="none"> <col class="field-name" /> <col class="field-body" /> <tbody valign="top"> <tr class="field-odd field"><th class="field-name">Parameters :</th><td class="field-body"><p class="first"><strong>tag</strong> : str</p> <blockquote class="last"> <div><p>Element name. If given, the tag must match the start tag. If omitted, the current element is closed.</p> </div></blockquote> </td> </tr> </tbody> </table> </dd></dl> <dl class="method"> <dt id="astropy.utils.xml.writer.XMLWriter.flush"> <tt class="descname">flush</tt><big>(</big><big>)</big><a class="reference external" href="http://github.com/astropy/astropy/tree/v0.2.4/astropy/utils/xml/writer.py#L237" title="Push the Edit button on the next page"><span class="edit-on-github viewcode-link"> [edit on github]</span></a><a class="reference internal" href="../_modules/astropy/utils/xml/writer.html#XMLWriter.flush"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#astropy.utils.xml.writer.XMLWriter.flush" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> <dl class="method"> <dt id="astropy.utils.xml.writer.XMLWriter.get_indentation"> <tt class="descname">get_indentation</tt><big>(</big><big>)</big><a class="reference external" href="http://github.com/astropy/astropy/tree/v0.2.4/astropy/utils/xml/writer.py#L240" title="Push the Edit button on the next page"><span class="edit-on-github viewcode-link"> [edit on github]</span></a><a class="reference internal" href="../_modules/astropy/utils/xml/writer.html#XMLWriter.get_indentation"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#astropy.utils.xml.writer.XMLWriter.get_indentation" title="Permalink to this definition">¶</a></dt> <dd><p>Returns the number of indentation levels the file is currently in.</p> </dd></dl> <dl class="method"> <dt id="astropy.utils.xml.writer.XMLWriter.get_indentation_spaces"> <tt class="descname">get_indentation_spaces</tt><big>(</big><em>offset=0</em><big>)</big><a class="reference external" href="http://github.com/astropy/astropy/tree/v0.2.4/astropy/utils/xml/writer.py#L247" title="Push the Edit button on the next page"><span class="edit-on-github viewcode-link"> [edit on github]</span></a><a class="reference internal" href="../_modules/astropy/utils/xml/writer.html#XMLWriter.get_indentation_spaces"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#astropy.utils.xml.writer.XMLWriter.get_indentation_spaces" title="Permalink to this definition">¶</a></dt> <dd><p>Returns a string of spaces that matches the current indentation level.</p> </dd></dl> <dl class="staticmethod"> <dt id="astropy.utils.xml.writer.XMLWriter.object_attrs"> <em class="property">static </em><tt class="descname">object_attrs</tt><big>(</big><em>obj</em>, <em>attrs</em><big>)</big><a class="reference external" href="http://github.com/astropy/astropy/tree/v0.2.4/astropy/utils/xml/writer.py#L254" title="Push the Edit button on the next page"><span class="edit-on-github viewcode-link"> [edit on github]</span></a><a class="reference internal" href="../_modules/astropy/utils/xml/writer.html#XMLWriter.object_attrs"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#astropy.utils.xml.writer.XMLWriter.object_attrs" title="Permalink to this definition">¶</a></dt> <dd><p>Converts an object with a bunch of attributes on an object into a dictionary for use by the <a class="reference internal" href="#astropy.utils.xml.writer.XMLWriter" title="astropy.utils.xml.writer.XMLWriter"><tt class="xref py py-obj docutils literal"><span class="pre">XMLWriter</span></tt></a>.</p> <table class="docutils field-list" frame="void" rules="none"> <col class="field-name" /> <col class="field-body" /> <tbody valign="top"> <tr class="field-odd field"><th class="field-name">Parameters :</th><td class="field-body"><p class="first"><strong>obj</strong> : object</p> <blockquote> <div><p>Any Python object</p> </div></blockquote> <p><strong>attrs</strong> : sequence of str</p> <blockquote> <div><p>Attribute names to pull from the object</p> </div></blockquote> </td> </tr> <tr class="field-even field"><th class="field-name">Returns :</th><td class="field-body"><p class="first"><strong>attrs</strong> : dict</p> <blockquote class="last"> <div><p>Maps attribute names to the values retrieved from <tt class="xref py py-obj docutils literal"><span class="pre">obj.attr</span></tt>. If any of the attributes is <tt class="xref py py-obj docutils literal"><span class="pre">None</span></tt>, it will not appear in the output dictionary.</p> </div></blockquote> </td> </tr> </tbody> </table> </dd></dl> <dl class="method"> <dt id="astropy.utils.xml.writer.XMLWriter.start"> <tt class="descname">start</tt><big>(</big><em>tag</em>, <em>attrib={}</em>, <em>**extra</em><big>)</big><a class="reference external" href="http://github.com/astropy/astropy/tree/v0.2.4/astropy/utils/xml/writer.py#L102" title="Push the Edit button on the next page"><span class="edit-on-github viewcode-link"> [edit on github]</span></a><a class="reference internal" href="../_modules/astropy/utils/xml/writer.html#XMLWriter.start"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#astropy.utils.xml.writer.XMLWriter.start" title="Permalink to this definition">¶</a></dt> <dd><p>Opens a new element. Attributes can be given as keyword arguments, or as a string/string dictionary. The method returns an opaque identifier that can be passed to the <a class="reference internal" href="#astropy.utils.xml.writer.XMLWriter.close" title="astropy.utils.xml.writer.XMLWriter.close"><tt class="xref py py-meth docutils literal"><span class="pre">close()</span></tt></a> method, to close all open elements up to and including this one.</p> <table class="docutils field-list" frame="void" rules="none"> <col class="field-name" /> <col class="field-body" /> <tbody valign="top"> <tr class="field-odd field"><th class="field-name">Parameters :</th><td class="field-body"><p class="first"><strong>tag</strong> : str</p> <blockquote> <div><p>The element name</p> </div></blockquote> <p><strong>attrib</strong> : dict of str -> str</p> <blockquote> <div><p>Attribute dictionary. Alternatively, attributes can be given as keyword arguments.</p> </div></blockquote> </td> </tr> <tr class="field-even field"><th class="field-name">Returns :</th><td class="field-body"><p class="first"><strong>id</strong> : int</p> <blockquote class="last"> <div><p>Returns an element identifier.</p> </div></blockquote> </td> </tr> </tbody> </table> </dd></dl> <dl class="method"> <dt id="astropy.utils.xml.writer.XMLWriter.tag"> <tt class="descname">tag</tt><big>(</big><em>*args</em>, <em>**kwds</em><big>)</big><a class="reference external" href="http://github.com/astropy/astropy/tree/v0.2.4/astropy/utils/xml/writer.py#L82" title="Push the Edit button on the next page"><span class="edit-on-github viewcode-link"> [edit on github]</span></a><a class="reference internal" href="../_modules/astropy/utils/xml/writer.html#XMLWriter.tag"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#astropy.utils.xml.writer.XMLWriter.tag" title="Permalink to this definition">¶</a></dt> <dd><p>A convenience method for use with the <tt class="xref py py-obj docutils literal"><span class="pre">with</span></tt> statement:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">with</span> <span class="n">writer</span><span class="o">.</span><span class="n">tag</span><span class="p">(</span><span class="s">'foo'</span><span class="p">):</span> <span class="n">writer</span><span class="o">.</span><span class="n">element</span><span class="p">(</span><span class="s">'bar'</span><span class="p">)</span> <span class="c"># </foo> is implicitly closed here</span> </pre></div> </div> <p>Parameters are the same as to <a class="reference internal" href="#astropy.utils.xml.writer.XMLWriter.start" title="astropy.utils.xml.writer.XMLWriter.start"><tt class="xref py py-obj docutils literal"><span class="pre">start</span></tt></a>.</p> </dd></dl> </dd></dl> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"><h3>Page Contents</h3> <ul> <li><a class="reference internal" href="#">XMLWriter</a></li> </ul> </div> </div> <div class="clearer"></div> </div> <footer class="footer"> <p class="pull-right"> <a href="../_sources/_generated/astropy.utils.xml.writer.XMLWriter.txt" rel="nofollow">Page Source</a> <a href="#">Back to Top</a></p> <p> © Copyright 2011-2013, The Astropy Developers.<br/> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. Last built 22 Oct 2013. <br/> </p> </footer> </body> </html>