I am developing a simple xml logging class using lxml in Python.
So far, my approach has been to use two files. A well-formed XML file that includes a second file that is an XML fragment. I am using the xi: include element. In this way, the XML fragment can be effectively updated by simply adding <event> elements to the end of the file.
A well-formed XML file ("logfile.xml") looks like this:
<?xml version="1.0"?> <logfile> <event xmlns:xi="http://www.w3.org/2001/XInclude"> <xi:include href="events.xml"/> </event> </logfile>
The XML fragment ('events.xml') looks like this:
<event> <data></data> </event> <event> <data></data> </event> <event> <data></data> </event>
My goal is to:
<?xml version="1.0"?> <logfile> <event> <data></data> </event> <event> <data></data> </event> <event> <data></data> </event> </logfile>
In python, I use the xinclude method to process the xi: include element in my well-formed XML file ('logfile.xml'). This works , but only if there is one <event> element is the XML fragment ('events.xml')
My python code is:
tree = etree.parse('logfile.xml') tree.xinclude() root = tree.getroot() print etree.tostring(self.logfileNode, pretty_print=True, xml_declaration=True, encoding='UTF-8')
The error I see is:
lxml.etree.XIncludeError: Extra content at the end of the document
I could have contained events in another element, but it defies adding data to the end of the XML fragment document.
source share