Working with Python lxml parser for creating XML elements

lxml parser can be a bit confusing because of the sheer range of options it offers. Here are a few cookbook style examples.

XML Generation

Target code:

Ok, Here is the code to generate it:

Notice the nsmap = {None: XHTML_NAMESPACE} line in etree.Element('root_element', nsmap = {None: XHTML_NAMESPACE}, some_more_params="12345-678-ABC", yet_more_params="POKEMON-SUCKS"). This is required if you wish to provide a namespace.

Note:If you passed it like so etree.Element('root_element', nsmap, some_more_params="12345-678-ABC", yet_more_params="POKEMON-SUCKS") with nsmap = {None: XHTML_NAMESPACE}, i.e. nsmap as a variable, you will get an error.




No Comments


You can leave the first : )



Leave a Reply

Your email address will not be published. Required fields are marked *