From 6097cd3f49b109c3474f19c30608c0708c1ec85b Mon Sep 17 00:00:00 2001 From: Parfait Gasana Date: Mon, 1 Mar 2021 21:39:25 -0600 Subject: [PATCH] BUG: Add decl when xml_declaration=True and pretty_print=False for etree parser --- pandas/io/formats/xml.py | 21 ++++++++++++++++++++- pandas/tests/io/xml/test_to_xml.py | 14 ++++++++------ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/pandas/io/formats/xml.py b/pandas/io/formats/xml.py index 044b03ba83714..dd68f0f78261e 100644 --- a/pandas/io/formats/xml.py +++ b/pandas/io/formats/xml.py @@ -324,7 +324,9 @@ def build_tree(self) -> bytes: if self.pretty_print: self.out_xml = self.prettify_tree() - if not self.xml_declaration: + if self.xml_declaration: + self.out_xml = self.add_declaration() + else: self.out_xml = self.remove_declaration() if self.stylesheet is not None: @@ -416,6 +418,23 @@ def prettify_tree(self) -> bytes: return dom.toprettyxml(indent=" ", encoding=self.encoding) + def add_declaration(self) -> bytes: + """ + Add xml declaration. + + This method will add xml declaration of working tree. Currently, + xml_declaration is supported in etree starting in Python 3.8. + """ + decl = f'\n' + + doc = ( + self.out_xml + if self.out_xml.startswith(b" bytes: """ Remove xml declaration. diff --git a/pandas/tests/io/xml/test_to_xml.py b/pandas/tests/io/xml/test_to_xml.py index 2026035a23370..97793ce8f65b8 100644 --- a/pandas/tests/io/xml/test_to_xml.py +++ b/pandas/tests/io/xml/test_to_xml.py @@ -867,8 +867,7 @@ def test_xml_declaration_pretty_print(): assert output == expected -@td.skip_if_no("lxml") -def test_no_pretty_print_with_decl(): +def test_no_pretty_print_with_decl(parser): expected = ( "\n" "0square" @@ -879,7 +878,7 @@ def test_no_pretty_print_with_decl(): "" ) - output = geom_df.to_xml(pretty_print=False, parser="lxml") + output = geom_df.to_xml(pretty_print=False, parser=parser) output = equalize_decl(output) # etree adds space for closed tags @@ -889,8 +888,7 @@ def test_no_pretty_print_with_decl(): assert output == expected -@td.skip_if_no("lxml") -def test_no_pretty_print_no_decl(): +def test_no_pretty_print_no_decl(parser): expected = ( "0square" "3604.0" @@ -900,7 +898,11 @@ def test_no_pretty_print_no_decl(): "" ) - output = geom_df.to_xml(xml_declaration=False, pretty_print=False) + output = geom_df.to_xml(xml_declaration=False, pretty_print=False, parser=parser) + + # etree adds space for closed tags + if output is not None: + output = output.replace(" />", "/>") assert output == expected