6 from utils.io
import open_write, ENCODING
9 from xml.etree.cElementTree
import Element, SubElement, parse, dump, ElementTree, fromstring, tostring, XML
11 from xml.etree.ElementTree
import Element, SubElement, parse, dump, ElementTree, fromstring, tostring, XML
14 """! @brief Return a pretty-printed XML string for the given XML element.
15 @param element An XML element.
16 @param encoding Encoding mode. Default value is 'utf-8'.
17 @return A Python string containing the printed version of the XML element.
19 from xml.dom
import minidom
20 rough_string = tostring(element, encoding=encoding)
21 reparsed = minidom.parseString(rough_string)
22 return reparsed.toprettyxml(indent=
" ", encoding=encoding)
25 """! @brief Write an XML element into a pretty XML output file.
26 @param element An XML element.
27 @param filename The name of the XML file to write with full path, for instance 'output.xml'.
28 @param encoding Encoding mode. Default value is 'utf-8'.
30 unicode_str =
prettify(element, encoding=encoding)
31 output_file =
open_write(filename, encoding=encoding)
32 output_file.write(unicode_str.decode(encoding))
36 """! @brief Parse an XML file.
37 @param filename The name of the XML file to parse with full path, for instance 'input.xml'.
38 @return The root XML element.
40 tree = parse(filename)
def open_write
Open file in write mode (automatically decode file in unicode).