xml data or other?

Stefan Behnel stefan_ml at behnel.de
Tue Nov 20 00:48:20 EST 2012


Prasad, Ramit, 19.11.2012 22:42:
> Artie Ziff wrote:
>> Writing XML files so to see whats happening. My plan is to
>> keep xml data in memory and parse with xml.etree.ElementTree.
>>
>> Unfortunately, xml parsing fails due to angle brackets inside
>> description tags. In particular, xml.etree.ElementTree.parse()
>> aborts on '<' inside xml data such as the following:
>>
>> <testname name="cron_test.sh">
>>      <description>
>>          This testcase tests if crontab <filename> installs the cronjob
>>          and cron schedules the job correctly.
>>      <\description>
>>
>> ##
>>
>> What is right way to handle the extra angle brackets?
>> Substitute on line-by-line basis, if that works?
>> Or learn to write a simple stack-style parser, or
>> recursive descent, it may be called?
> 
> I think your description text should be in a CDATA section.
> http://en.wikipedia.org/wiki/CDATA#CDATA_sections_in_XML

Ah, don't bother with CDATA. Just make sure the data gets properly escaped,
any XML serialiser will do that for you. Just generate the XML using
ElementTree and you'll be fine. Generating XML as literal text is not a
good idea.

Stefan





More information about the Python-list mailing list