<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: An easy way to write XML in Python</title>
	<atom:link href="http://ginstrom.com/scribbles/2009/01/07/an-easy-way-to-write-xml-in-python/feed/" rel="self" type="application/rss+xml" />
	<link>http://ginstrom.com/scribbles/2009/01/07/an-easy-way-to-write-xml-in-python/</link>
	<description>Random scribbling about programming, translation, and Japan</description>
	<lastBuildDate>Thu, 10 May 2012 05:41:14 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
	<item>
		<title>By: Ryan Ginstrom</title>
		<link>http://ginstrom.com/scribbles/2009/01/07/an-easy-way-to-write-xml-in-python/comment-page-1/#comment-16848</link>
		<dc:creator>Ryan Ginstrom</dc:creator>
		<pubDate>Fri, 02 Apr 2010 15:27:08 +0000</pubDate>
		<guid isPermaLink="false">http://ginstrom.com/scribbles/?p=766#comment-16848</guid>
		<description>@Holger

Thanks!</description>
		<content:encoded><![CDATA[<p>@Holger</p>
<p>Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Holger Joukl</title>
		<link>http://ginstrom.com/scribbles/2009/01/07/an-easy-way-to-write-xml-in-python/comment-page-1/#comment-16718</link>
		<dc:creator>Holger Joukl</dc:creator>
		<pubDate>Mon, 29 Mar 2010 14:37:34 +0000</pubDate>
		<guid isPermaLink="false">http://ginstrom.com/scribbles/?p=766#comment-16718</guid>
		<description>You can strip annotations using objectify.deannotate():

&gt;&gt;&gt; msg = objectify.Element(&#039;msg&#039;)
msg = None [ObjectifiedElement]
&gt;&gt;&gt; msg.s = &quot;somestring&quot;
&gt;&gt;&gt; print etree.tostring(msg, pretty_print=True)

  somestring


&gt;&gt;&gt; objectify.deannotate(msg) # strip type annotations
&gt;&gt;&gt; print etree.tostring(msg, pretty_print=True)

  somestring


&gt;&gt;&gt; # maybe also strip unused namespaces
&gt;&gt;&gt; etree.cleanup_namespaces(msg)
&gt;&gt;&gt; print etree.tostring(msg, pretty_print=True)

  somestring


&gt;&gt;&gt;</description>
		<content:encoded><![CDATA[<p>You can strip annotations using objectify.deannotate():</p>
<p>&gt;&gt;&gt; msg = objectify.Element(&#8216;msg&#8217;)<br />
msg = None [ObjectifiedElement]<br />
&gt;&gt;&gt; msg.s = &#8220;somestring&#8221;<br />
&gt;&gt;&gt; print etree.tostring(msg, pretty_print=True)</p>
<p>  somestring</p>
<p>&gt;&gt;&gt; objectify.deannotate(msg) # strip type annotations<br />
&gt;&gt;&gt; print etree.tostring(msg, pretty_print=True)</p>
<p>  somestring</p>
<p>&gt;&gt;&gt; # maybe also strip unused namespaces<br />
&gt;&gt;&gt; etree.cleanup_namespaces(msg)<br />
&gt;&gt;&gt; print etree.tostring(msg, pretty_print=True)</p>
<p>  somestring</p>
<p>&gt;&gt;&gt;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ryan Ginstrom</title>
		<link>http://ginstrom.com/scribbles/2009/01/07/an-easy-way-to-write-xml-in-python/comment-page-1/#comment-515</link>
		<dc:creator>Ryan Ginstrom</dc:creator>
		<pubDate>Wed, 07 Jan 2009 22:49:02 +0000</pubDate>
		<guid isPermaLink="false">http://ginstrom.com/scribbles/?p=766#comment-515</guid>
		<description>@Peter

Thanks for the link. I didn&#039;t know you could use lxml.objectify to create xml as well. But testing it out, lxml seems to be adding type annotations and schema info (I&#039;m using 2.2 beta 1). Is there a way to suppress that?

&quot;...&lt;name py:pytype=&quot;str&quot;&gt;Ryan&lt;/name&gt;...&quot;</description>
		<content:encoded><![CDATA[<p>@Peter</p>
<p>Thanks for the link. I didn&#8217;t know you could use lxml.objectify to create xml as well. But testing it out, lxml seems to be adding type annotations and schema info (I&#8217;m using 2.2 beta 1). Is there a way to suppress that?</p>
<p>&#8220;&#8230;&lt;name py:pytype=&#8221;str&#8221;&gt;Ryan&lt;/name&gt;&#8230;&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter</title>
		<link>http://ginstrom.com/scribbles/2009/01/07/an-easy-way-to-write-xml-in-python/comment-page-1/#comment-519</link>
		<dc:creator>Peter</dc:creator>
		<pubDate>Wed, 07 Jan 2009 19:34:29 +0000</pubDate>
		<guid isPermaLink="false">http://ginstrom.com/scribbles/?p=766#comment-519</guid>
		<description>As already said on http://stackoverflow.com/questions/418497/how-do-i-convert-xml-to-nested-objects#419232 just have a look at http://codespeak.net/lxml/objectify.html it is really great:

&gt;&gt;&gt; xml = &quot;&quot;&quot;&lt;main&gt;
... &lt;object1 attr=&quot;name&quot;&gt;content&quot;&lt;/object1&gt;
... &lt;object1 attr=&quot;foo&quot;&gt;contenbar&quot;&lt;/object1&gt;
... &lt;test&gt;me&quot;&lt;/test&gt;
... &lt;/main&gt;&quot;&quot;&quot;


&gt;&gt;&gt; from lxml import objectify

&gt;&gt;&gt; main = objectify.fromstring(xml)

&gt;&gt;&gt; main.object1[0]
&#039;content&#039;

&gt;&gt;&gt; main.object1[1]
&#039;contenbar&#039;

&gt;&gt;&gt; main.object1[0].get(&quot;attr&quot;)
&#039;name&#039;

&gt;&gt;&gt; main.test
&#039;me&#039;</description>
		<content:encoded><![CDATA[<p>As already said on <a href="http://stackoverflow.com/questions/418497/how-do-i-convert-xml-to-nested-objects#419232">http://stackoverflow.com/questions/418497/how-do-i-convert-xml-to-nested-objects#419232</a> just have a look at <a href="http://codespeak.net/lxml/objectify.html">http://codespeak.net/lxml/objectify.html</a> it is really great:</p>
<p>&gt;&gt;&gt; xml = &#8220;&#8221;"&lt;main&gt;<br />
&#8230; &lt;object1 attr=&#8221;name&#8221;&gt;content&#8221;&lt;/object1&gt;<br />
&#8230; &lt;object1 attr=&#8221;foo&#8221;&gt;contenbar&#8221;&lt;/object1&gt;<br />
&#8230; &lt;test&gt;me&#8221;&lt;/test&gt;<br />
&#8230; &lt;/main&gt;&#8221;"&#8221;</p>
<p>&gt;&gt;&gt; from lxml import objectify</p>
<p>&gt;&gt;&gt; main = objectify.fromstring(xml)</p>
<p>&gt;&gt;&gt; main.object1[0]<br />
&#8216;content&#8217;</p>
<p>&gt;&gt;&gt; main.object1[1]<br />
&#8216;contenbar&#8217;</p>
<p>&gt;&gt;&gt; main.object1[0].get(&#8220;attr&#8221;)<br />
&#8216;name&#8217;</p>
<p>&gt;&gt;&gt; main.test<br />
&#8216;me&#8217;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ryan Ginstrom</title>
		<link>http://ginstrom.com/scribbles/2009/01/07/an-easy-way-to-write-xml-in-python/comment-page-1/#comment-520</link>
		<dc:creator>Ryan Ginstrom</dc:creator>
		<pubDate>Wed, 07 Jan 2009 17:08:33 +0000</pubDate>
		<guid isPermaLink="false">http://ginstrom.com/scribbles/?p=766#comment-520</guid>
		<description>@Kamil

The problem with your proposal is that you can&#039;t override the assignment operator in Python. Thus you&#039;d need some serious voodoo to stick children under those li nodes.

I&#039;m thinking of syntax along these lines (borrowing from gnosis):

&lt;pre lang=&quot;python&quot;&gt;
root.nodes.add_node(XmlNode(&quot;node&quot;, u&quot;spam&quot;))
root.nodes.add_node(XmlNode(&quot;node&quot;))
root.nodes.node[1].val = u&quot;egg&quot;&lt;/pre&gt;

@J. Ruigrok

I think we&#039;re in agreement that this is the quick-and-easy way to create XML, not the enterprise-ready way.

To me, creating XML in Python is a big pain point. With any of the tools out there that I know of, it gets painful and messy really quickly. Sometimes I just want something simple and relatively painless. I could see using lxml or the like under the hood, but I explicitly don&#039;t want to add a lot of complexity to the interface.</description>
		<content:encoded><![CDATA[<p>@Kamil</p>
<p>The problem with your proposal is that you can&#8217;t override the assignment operator in Python. Thus you&#8217;d need some serious voodoo to stick children under those li nodes.</p>
<p>I&#8217;m thinking of syntax along these lines (borrowing from gnosis):</p>
<pre lang="python">
root.nodes.add_node(XmlNode("node", u"spam"))
root.nodes.add_node(XmlNode("node"))
root.nodes.node[1].val = u"egg"</pre>
<p>@J. Ruigrok</p>
<p>I think we&#8217;re in agreement that this is the quick-and-easy way to create XML, not the enterprise-ready way.</p>
<p>To me, creating XML in Python is a big pain point. With any of the tools out there that I know of, it gets painful and messy really quickly. Sometimes I just want something simple and relatively painless. I could see using lxml or the like under the hood, but I explicitly don&#8217;t want to add a lot of complexity to the interface.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kamil Kisiel</title>
		<link>http://ginstrom.com/scribbles/2009/01/07/an-easy-way-to-write-xml-in-python/comment-page-1/#comment-521</link>
		<dc:creator>Kamil Kisiel</dc:creator>
		<pubDate>Wed, 07 Jan 2009 16:52:47 +0000</pubDate>
		<guid isPermaLink="false">http://ginstrom.com/scribbles/?p=766#comment-521</guid>
		<description>How about the following for lists:

div.ul.li = [&quot;first item&quot;, &quot;second item&quot;]

Of course that only supports a homogeneous array of child tags.</description>
		<content:encoded><![CDATA[<p>How about the following for lists:</p>
<p>div.ul.li = ["first item", "second item"]</p>
<p>Of course that only supports a homogeneous array of child tags.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: J. Ruigrok van der Werven</title>
		<link>http://ginstrom.com/scribbles/2009/01/07/an-easy-way-to-write-xml-in-python/comment-page-1/#comment-523</link>
		<dc:creator>J. Ruigrok van der Werven</dc:creator>
		<pubDate>Wed, 07 Jan 2009 16:11:36 +0000</pubDate>
		<guid isPermaLink="false">http://ginstrom.com/scribbles/?p=766#comment-523</guid>
		<description>Strange, my comment didn&#039;t go through I guess.

Anyway, I pointed out that there&#039;s lxml and ElementTree (etree) that might be useful for you without having to roll your own solutions like this.

Since 2.5 Python has xml.etree, but there&#039;s still an external version being updated and maintained.</description>
		<content:encoded><![CDATA[<p>Strange, my comment didn&#8217;t go through I guess.</p>
<p>Anyway, I pointed out that there&#8217;s lxml and ElementTree (etree) that might be useful for you without having to roll your own solutions like this.</p>
<p>Since 2.5 Python has xml.etree, but there&#8217;s still an external version being updated and maintained.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ryan Ginstrom</title>
		<link>http://ginstrom.com/scribbles/2009/01/07/an-easy-way-to-write-xml-in-python/comment-page-1/#comment-522</link>
		<dc:creator>Ryan Ginstrom</dc:creator>
		<pubDate>Wed, 07 Jan 2009 15:58:02 +0000</pubDate>
		<guid isPermaLink="false">http://ginstrom.com/scribbles/?p=766#comment-522</guid>
		<description>You&#039;re definitely right that things like lists are a big weakness in the module. I&#039;ll probably try to add ways to handle that at some point.

But then again, for a complex document I&#039;m more likely to use a templating language than to generate the XML tree in code. I guess that&#039;s just me, though. :)</description>
		<content:encoded><![CDATA[<p>You&#8217;re definitely right that things like lists are a big weakness in the module. I&#8217;ll probably try to add ways to handle that at some point.</p>
<p>But then again, for a complex document I&#8217;m more likely to use a templating language than to generate the XML tree in code. I guess that&#8217;s just me, though. <img src='http://ginstrom.com/scribbles/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Adam</title>
		<link>http://ginstrom.com/scribbles/2009/01/07/an-easy-way-to-write-xml-in-python/comment-page-1/#comment-518</link>
		<dc:creator>Adam</dc:creator>
		<pubDate>Wed, 07 Jan 2009 15:24:44 +0000</pubDate>
		<guid isPermaLink="false">http://ginstrom.com/scribbles/?p=766#comment-518</guid>
		<description>So it has, I didn&#039;t notice the comment getting mangled.

I think one of the main problems is that the XML standard is easy to understand the basics but the full specification is pretty complicated. I used libxml2 as the main engine since it&#039;s pretty much watertight being the Gnome XML library.

I think you may run into limitations quite quickly using instances as nodes. For example, how would you generate an XHTML list?

div = XmlNode(&quot;container&quot;)
div.ul.li = &quot;First Item&quot;
div.ul.li = &quot;About to overwrite the first item?&quot;

I tinkered with serializing Python objects to XML a while back and came to the conclusion that the limitations outweigh the usefulness.</description>
		<content:encoded><![CDATA[<p>So it has, I didn&#8217;t notice the comment getting mangled.</p>
<p>I think one of the main problems is that the XML standard is easy to understand the basics but the full specification is pretty complicated. I used libxml2 as the main engine since it&#8217;s pretty much watertight being the Gnome XML library.</p>
<p>I think you may run into limitations quite quickly using instances as nodes. For example, how would you generate an XHTML list?</p>
<p>div = XmlNode(&#8220;container&#8221;)<br />
div.ul.li = &#8220;First Item&#8221;<br />
div.ul.li = &#8220;About to overwrite the first item?&#8221;</p>
<p>I tinkered with serializing Python objects to XML a while back and came to the conclusion that the limitations outweigh the usefulness.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ryan Ginstrom</title>
		<link>http://ginstrom.com/scribbles/2009/01/07/an-easy-way-to-write-xml-in-python/comment-page-1/#comment-517</link>
		<dc:creator>Ryan Ginstrom</dc:creator>
		<pubDate>Wed, 07 Jan 2009 14:48:24 +0000</pubDate>
		<guid isPermaLink="false">http://ginstrom.com/scribbles/?p=766#comment-517</guid>
		<description>I think you&#039;ve kind of proven your point, because the comment monster seems to have eaten your angle brackets.

There are lots of options for escaping XML entities in Python. &lt;a href=&quot;http://wiki.python.org/moin/EscapingXml&quot;&gt;Here&#039;s a page on it&lt;/a&gt; from the Python wiki. I&#039;m not entirely convinced that the module should be escaping xml, but I agree that it would be simpler for the user.

I&#039;m sure the solution you wrote is more correct, but pardon me for saying -- it doesn&#039;t look incredibly simple to use.

I just want to do:

&lt;pre lang=&quot;python&quot;&gt;
prefs = XmlNode(&quot;prefs&quot;)
prefs.language.val = u&quot;English&quot;
prefs.user.name.val = u&quot;Ryan&quot;
prefs.user.email.val = u&quot;ryan@example.com&quot;
prefs.user[&quot;id&quot;] = u&quot;123&quot;
prefs.onions.val = u&quot;extra&quot;&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>I think you&#8217;ve kind of proven your point, because the comment monster seems to have eaten your angle brackets.</p>
<p>There are lots of options for escaping XML entities in Python. <a href="http://wiki.python.org/moin/EscapingXml">Here&#8217;s a page on it</a> from the Python wiki. I&#8217;m not entirely convinced that the module should be escaping xml, but I agree that it would be simpler for the user.</p>
<p>I&#8217;m sure the solution you wrote is more correct, but pardon me for saying &#8212; it doesn&#8217;t look incredibly simple to use.</p>
<p>I just want to do:</p>
<pre lang="python">
prefs = XmlNode("prefs")
prefs.language.val = u"English"
prefs.user.name.val = u"Ryan"
prefs.user.email.val = u"ryan@example.com"
prefs.user["id"] = u"123"
prefs.onions.val = u"extra"</pre>
]]></content:encoded>
	</item>
</channel>
</rss>

