[BangPypers] Empty element in xml
Gora Mohanty
gora at mimirtech.com
Mon Nov 4 04:16:51 CET 2013
On 4 November 2013 03:00, Amit Sethi <amit.pureenergy at gmail.com> wrote:
>
> Hi All, I am stuck with wierd problem . I am dealing with a legacy
> system(written in C) that deals with using some xml. The xml is
> created with python. Now the trouble is that the legacy system
> understands empty element as:
>
> <element><element>
>
> where as using python libs lxml or xml.etree or anything empty element
> is written as
> <element/> is there a way I can change that. So that I don't have to
> make any changes in the legacy system.
Have pretty much quit using Python XML parsers other than lxml,
but lxml can both read and write such tags. I would be surprised if
xml.etree could not do that.
from lxml import etree
# Read:
xml = '<a></a>'
root = etree.fromstring( xml )
print etree.tostring( root )
# Write:
root = etree.Element( 'doc' )
child = etree.SubElement( root, 'a' )
# Set empty string as element content: Open/close tags forced
child.text = ''
print etree.tostring( root )
Regards,
Gora
More information about the BangPypers
mailing list