[lxml-dev] parsing and serializing XML fragments
Hervé Cauwelier
herve.cauwelier at free.fr
Thu Jun 25 11:29:07 CEST 2009
Stefan Behnel a écrit :
> Hi,
>
> Hervé Cauwelier wrote:
>> Hi, I'm trying to load fragments of XML to inject them in an existing
>> document tree.
>>
>> They look like this:
>>
>> <table:table table:name="%s" table:style-name="%s"/>
>
> Just curious: why do you create a document in which you can do string
> replacements?
I have templates for paragraphgs, lists, tables, etc. You just provide
its name, style, text contents, whatever applies.
> But you can insert your document into a new root Element that defines all
> used namespaces (either fixed or collected at runtime), serialise that, and
> strip the root element from top and bottom of the serialised byte string. I
> do a bit of string mangling in lxml's own doctests to make them work in
> both Py2 and Py3, it's not that hard to add these things. You can write a
> little wrapper class around lxml.etree and override tostring() and parse()
> to fit your needs (kudos to Fredrik for making them functions, BTW).
>
> Here's an example:
>
> http://codespeak.net/svn/lxml/trunk/doc/api.txt
Thanks, I'll look deeper into the guts of lxml.
More information about the lxml-dev
mailing list