[lxml-dev] An lxml tree inside a lxml tree.

John Lovell jlovell at esd189.org
Wed Mar 26 16:12:47 CET 2008


Based on Stefan's previous answer to me or the tutorial (blush).

Code:

from lxml import etree
from StringIO import StringIO

top = etree.Element("root")

xml = """<subroot>
  <child1/>
  <child2/>
  <child3/>
</subroot>
"""

parser = etree.XMLParser(remove_blank_text=True)

# It doesn't matter which one of these I use.
root = etree.fromstring(xml, parser)
#root = etree.parse(StringIO(xml), parser).getroot()

print root.tag

top.append(root)

print ""
print etree.tostring(top, pretty_print=False)
print ""
print etree.tostring(top, pretty_print=True)

Output:

subroot

<root><subroot><child1/><child2/><child3/></subroot></root>

<root>
  <subroot>
    <child1/>
    <child2/>
    <child3/>
  </subroot>
</root>


Hope this helps,

John W. Lovell
Web Applications Engineer
Northwest Educational Service District
1601 R Avenue
Anacortes, WA 98221
 
www.esd189.org
Together We Can ...

-----Original Message-----
From: lxml-dev-bounces at codespeak.net
[mailto:lxml-dev-bounces at codespeak.net] On Behalf Of Albert Brandl
Sent: Wednesday, March 26, 2008 5:34 AM
To: Stefan Behnel
Cc: lxml-dev at codespeak.net
Subject: Re: [lxml-dev] An lxml tree inside a lxml tree.

On Wed, Mar 26, 2008 at 12:07:09PM +0100, Stefan Behnel wrote:
> I added an answer here:
> 
> https://answers.launchpad.net/lxml/+question/28032

I finally got it ;-). It was not clear to me that lxml only _adds_
whitespace when appropriate, neither what "appropriate" actually means.
If I understand your explanation correctly, the pretty-printing
algorithm leaves alone any elements that already contain whitespace -
this corresponds to the behavior in my example, and your method of
removing leading and trailing whitespace will help in most cases.

Thanks,

Albert
_______________________________________________
lxml-dev mailing list
lxml-dev at codespeak.net
http://codespeak.net/mailman/listinfo/lxml-dev


More information about the lxml-dev mailing list