@@ -143,14 +143,22 @@ def main():
143143 # Serialize XML to bytes with XML declaration, no pretty formatting
144144 serialized_xml = serialize_xml (new_root )
145145
146- # Write the serialized XML to file as binary
147- with open ("sitemap.xml" , "wb" ) as f :
148- f .write (serialized_xml )
149-
150- # Alternatively, if you prefer to write as text, decode the bytes
151- # serialized_xml_str = serialized_xml.decode('utf-8')
152- # with open("sitemap.xml", "w", encoding="utf-8") as f:
153- # f.write(serialized_xml_str)
146+ # Convert bytes to string and replace single quotes with double quotes in XML declaration
147+ serialized_xml_str = serialized_xml .decode ('utf-8' )
148+ if serialized_xml_str .startswith ("<?xml" ):
149+ # Replace single quotes with double quotes in the XML declaration only
150+ xml_declaration_end = serialized_xml_str .find ("?>" ) + 2
151+ xml_declaration = serialized_xml_str [:xml_declaration_end ]
152+ xml_declaration = xml_declaration .replace ("'" , '"' )
153+ rest_of_xml = serialized_xml_str [xml_declaration_end :]
154+ serialized_xml_str = xml_declaration + rest_of_xml
155+
156+ # Remove any newline or carriage return characters to ensure single-line XML
157+ serialized_xml_str = serialized_xml_str .replace ('\n ' , '' ).replace ('\r ' , '' )
158+
159+ # Write the serialized XML to file as text
160+ with open ("sitemap.xml" , "w" , encoding = "utf-8" ) as f :
161+ f .write (serialized_xml_str )
154162
155163if __name__ == "__main__" :
156164 main ()
0 commit comments