Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a doctype with lxml's etree

I want to add doctypes to my XML documents that I'm generating with LXML's etree.

However I cannot figure out how to add a doctype. Hardcoding and concating the string is not an option.

I was expecting something along the lines of how PI's are added in etree:

pi = etree.PI(...)
doc.addprevious(pi)

But it's not working for me. How to add a to a xml document with lxml?

like image 526
Marijn Avatar asked Jun 14 '09 00:06

Marijn


People also ask

What is Etree in lxml?

Parsing from strings and files. lxml. etree supports parsing XML in a number of ways and from all important sources, namely strings, files, URLs (http/ftp) and file-like objects. The main parse functions are fromstring() and parse(), both called with the source as first argument.

What is lxml HTML?

Introduction. The lxml XML toolkit is a Pythonic binding for the C libraries libxml2 and libxslt. It is unique in that it combines the speed and XML feature completeness of these libraries with the simplicity of a native Python API, mostly compatible but superior to the well-known ElementTree API.

Why is lxml used?

lxml is a Python library which allows for easy handling of XML and HTML files, and can also be used for web scraping. There are a lot of off-the-shelf XML parsers out there, but for better results, developers sometimes prefer to write their own XML and HTML parsers. This is when the lxml library comes to play.


1 Answers

This worked for me:

print etree.tostring(tree, pretty_print=True, xml_declaration=True, encoding="UTF-8", doctype="<!DOCTYPE TEST_FILE>")
like image 178
hgb Avatar answered Oct 10 '22 10:10

hgb