[lxml-dev] An lxml tree inside a lxml tree.
John Lovell
jlovell at esd189.org
Mon Mar 24 17:10:20 CET 2008
Stefan:
Of course coming from you this works great. I am sorry I missed this in
the tutorial.
John
-----Original Message-----
From: Stefan Behnel [mailto:stefan_ml at behnel.de]
Sent: Sunday, March 23, 2008 9:57 AM
To: John Lovell
Cc: lxml-dev at codespeak.net
Subject: Re: [lxml-dev] An lxml tree inside a lxml tree.
Hi,
John Lovell wrote:
> Okay, I came up with some sample code to illustrate my problem and the
> behavior shows up without included one tree inside another.
>
>
> Code:
>
> from lxml import etree
> from StringIO import StringIO
>
> xml = """<root>
> <child1/>
> <child2/>
> <child3/>
> </root>
> """
>
> # It doesn't matter which one of these I use.
> root = etree.fromstring(xml)
> #root = etree.parse(StringIO(xml)).getroot()
>
> print root.tag
>
> print ""
>
> print etree.tostring(root, pretty_print=False)
>
>
> Output:
>
> root
>
> <root>
> <child1/>
> <child2/>
> <child3/>
> </root>
lxml will not alter your document unless you tell it to do so. This
includes whitespace - which you may or may not consider ignorable. The
"pretty_print=False" option tells lxml to not modify the document on
serialisation, so it doesn't keeps the whitespace the way it is.
If you want the whitespace removed, tell the parser to do it for you
using the "remove_blank_text" option.
Stefan
More information about the lxml-dev
mailing list