Revision: 2e0e4e7badad
Author:   Janne Härkönen <[email protected]>
Date:     Fri Jun 29 02:01:34 2012
Log:      XML Library: more reliable way to convert element to string
http://code.google.com/p/robotframework/source/detail?r=2e0e4e7badad

Modified:
 /atest/testdata/standard_libraries/xml/to_string.txt
 /src/robot/libraries/XML.py

=======================================
--- /atest/testdata/standard_libraries/xml/to_string.txt Fri Jun 29 01:26:08 2012 +++ /atest/testdata/standard_libraries/xml/to_string.txt Fri Jun 29 02:01:34 2012
@@ -11,7 +11,7 @@
 Element to string
     ${text}=    Element To String    <simple>content</simple>
     Should Be Equal    ${text}    <simple>content</simple>
-    ${text}=    Element To String    ${TEST}    with_preamble=${True}
+    ${text}=    Element To String    ${TEST}    xml_declaration=${True}
     ${expected}=    Get File    ${TEST}
     # TODO: Fails because attributes are not ordered
     Should Be Equal    ${text.strip()}    ${expected.strip()}
=======================================
--- /src/robot/libraries/XML.py Fri Jun 29 01:26:08 2012
+++ /src/robot/libraries/XML.py Fri Jun 29 02:01:34 2012
@@ -16,6 +16,7 @@

 import re
 from functools import partial
+from StringIO import StringIO

 from robot.libraries.BuiltIn import BuiltIn
 from robot.api import logger
@@ -128,7 +129,9 @@
     def log_element(self, source, level='INFO'):
         logger.write(self.element_to_string(source), level)

-    def element_to_string(self, source, with_preamble=False):
-        method = 'xml' if with_preamble else 'html'
-        return ET.tostring(self.get_element(source),
-                           encoding='UTF-8', method=method).decode('UTF-8')
+    def element_to_string(self, source, xml_declaration=False):
+        tree = ET.ElementTree(self.get_element(source))
+        output = StringIO()
+ tree.write(output, encoding='UTF-8', xml_declaration=xml_declaration)
+        output.seek(0)
+        return output.read().decode('UTF-8')

Reply via email to