[BangPypers] Empty element in xml

Amit Sethi amit.pureenergy at gmail.com
Mon Nov 4 16:37:56 CET 2013


I ended up using method=html for writing the xml.

tree.write(filename, encoding="utf-8", xml_declaration=True, method='html')

I am not sure if there are going to be any problems with this but for
the time being my nosetests seem to be working fine


Thanks
Amit

On Mon, Nov 4, 2013 at 8:46 AM, Gora Mohanty <gora at mimirtech.com> wrote:
> 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
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> https://mail.python.org/mailman/listinfo/bangpypers



-- 
A-M-I-T S|S


More information about the BangPypers mailing list