@@ -36,7 +36,7 @@ def __init__(self):
3636self ._childNodes = []
3737
3838def appendChild (self ,element ):
39- warnings . warn ( "lxml does not support comments as siblings of the root node" , DataLossWarning )
39+ self . _elementTree . getroot (). addnext ( element . _element )
4040
4141def _getChildNodes (self ):
4242return self ._childNodes
@@ -50,11 +50,19 @@ def serializeElement(element, indent=0):
5050if not hasattr (element ,"tag" ):
5151rv .append ("#document" )
5252if element .docinfo .internalDTD :
53- dtd_str = element .docinfo .doctype
54- if not dtd_str :
53+ if not (element .docinfo .public_id or element .docinfo .system_url ):
5554dtd_str = "<!DOCTYPE %s>" % element .docinfo .root_name
55+ else :
56+ dtd_str = """<!DOCTYPE %s PUBLIC "%s" "%s">""" % (
57+ element .docinfo .root_name ,element .docinfo .public_id ,
58+ element .docinfo .system_url )
5659rv .append ("|%s%s" % (' ' * (indent + 2 ),dtd_str ))
57- serializeElement (element .getroot (),indent + 2 )
60+ next_element = element .getroot ()
61+ while next_element .getprevious ()is not None :
62+ next_element = next_element .getprevious ()
63+ while next_element is not None :
64+ serializeElement (next_element ,indent + 2 )
65+ next_element = next_element .getnext ()
5866elif type (element .tag )== type (etree .Comment ):
5967rv .append ("|%s<!-- %s -->" % (' ' * indent ,element .text ))
6068else :
@@ -136,6 +144,7 @@ def __init__(self, fullTree = False):
136144def reset (self ):
137145_base .TreeBuilder .reset (self )
138146self .insertComment = self .insertCommentInitial
147+ self .initial_comments = []
139148self .doctype = None
140149
141150def testSerializer (self ,element ):
@@ -159,7 +168,7 @@ def insertDoctype(self, name, publicId, systemId):
159168self .doctype = doctype
160169
161170def insertCommentInitial (self ,data ,parent = None ):
162- warnings . warn ( "lxml does not support comments as siblings of the root node" , DataLossWarning )
171+ self . initial_comments . append ( data )
163172
164173def insertRoot (self ,name ):
165174"""Create the document root"""
@@ -181,6 +190,10 @@ def insertRoot(self, name):
181190print docStr
182191raise
183192
193+ #Append the initial comments:
194+ for comment_data in self .initial_comments :
195+ root .addprevious (etree .Comment (comment_data ))
196+
184197#Create the root document and add the ElementTree to it
185198self .document = self .documentClass ()
186199self .document ._elementTree = root .getroottree ()