Skip to content

Commit f9248f0

Browse files
authored
BUG: Add decl when xml_declaration=True and pretty_print=False for etree parser (#40162)
1 parent 2aa1ab4 commit f9248f0

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

pandas/io/formats/xml.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,9 @@ def build_tree(self) -> bytes:
324324
if self.pretty_print:
325325
self.out_xml = self.prettify_tree()
326326

327-
if not self.xml_declaration:
327+
if self.xml_declaration:
328+
self.out_xml = self.add_declaration()
329+
else:
328330
self.out_xml = self.remove_declaration()
329331

330332
if self.stylesheet is not None:
@@ -416,6 +418,23 @@ def prettify_tree(self) -> bytes:
416418

417419
return dom.toprettyxml(indent=" ", encoding=self.encoding)
418420

421+
def add_declaration(self) -> bytes:
422+
"""
423+
Add xml declaration.
424+
425+
This method will add xml declaration of working tree. Currently,
426+
xml_declaration is supported in etree starting in Python 3.8.
427+
"""
428+
decl = f'<?xml version="1.0" encoding="{self.encoding}"?>\n'
429+
430+
doc = (
431+
self.out_xml
432+
if self.out_xml.startswith(b"<?xml")
433+
else decl.encode(self.encoding) + self.out_xml
434+
)
435+
436+
return doc
437+
419438
def remove_declaration(self) -> bytes:
420439
"""
421440
Remove xml declaration.

pandas/tests/io/xml/test_to_xml.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -867,8 +867,7 @@ def test_xml_declaration_pretty_print():
867867
assert output == expected
868868

869869

870-
@td.skip_if_no("lxml")
871-
def test_no_pretty_print_with_decl():
870+
def test_no_pretty_print_with_decl(parser):
872871
expected = (
873872
"<?xml version='1.0' encoding='utf-8'?>\n"
874873
"<data><row><index>0</index><shape>square</shape>"
@@ -879,7 +878,7 @@ def test_no_pretty_print_with_decl():
879878
"</row></data>"
880879
)
881880

882-
output = geom_df.to_xml(pretty_print=False, parser="lxml")
881+
output = geom_df.to_xml(pretty_print=False, parser=parser)
883882
output = equalize_decl(output)
884883

885884
# etree adds space for closed tags
@@ -889,8 +888,7 @@ def test_no_pretty_print_with_decl():
889888
assert output == expected
890889

891890

892-
@td.skip_if_no("lxml")
893-
def test_no_pretty_print_no_decl():
891+
def test_no_pretty_print_no_decl(parser):
894892
expected = (
895893
"<data><row><index>0</index><shape>square</shape>"
896894
"<degrees>360</degrees><sides>4.0</sides></row><row>"
@@ -900,7 +898,11 @@ def test_no_pretty_print_no_decl():
900898
"</row></data>"
901899
)
902900

903-
output = geom_df.to_xml(xml_declaration=False, pretty_print=False)
901+
output = geom_df.to_xml(xml_declaration=False, pretty_print=False, parser=parser)
902+
903+
# etree adds space for closed tags
904+
if output is not None:
905+
output = output.replace(" />", "/>")
904906

905907
assert output == expected
906908

0 commit comments

Comments
 (0)