@@ -56,7 +56,7 @@ def load_bson(filename: str):
56
56
data = bson .decode (data_encoded )
57
57
58
58
return data
59
-
59
+
60
60
61
61
def load_xml (filename : str ):
62
62
"""
@@ -100,6 +100,7 @@ def element_to_dict(element):
100
100
101
101
return result
102
102
103
+
103
104
def save_to_json_file (info_dict , filename , indent = 4 ):
104
105
105
106
strj = json .dumps (info_dict , indent = indent )
@@ -116,6 +117,7 @@ def save_to_yaml_file(info_dict, filename, indent=4):
116
117
with open (filename , "w" ) as fp :
117
118
fp .write (stry )
118
119
120
+
119
121
def save_to_xml_file (info_dict , filename , indent = 4 ):
120
122
"""
121
123
Save a dictionary to an XML file.
@@ -134,7 +136,7 @@ def save_to_xml_file(info_dict, filename, indent=4):
134
136
tree = ET .ElementTree (root )
135
137
136
138
# Generate the XML string
137
- xml_str = ET .tostring (root , encoding = "utf-8" ).decode ("utf-8" )
139
+ xml_str = ET .tostring (root , encoding = "utf-8" , method = "xml" ).decode ("utf-8" )
138
140
139
141
# Create a pretty-formatted XML string using minidom
140
142
dom = xml .dom .minidom .parseString (xml_str )
@@ -144,44 +146,47 @@ def save_to_xml_file(info_dict, filename, indent=4):
144
146
with open (filename , "w" , encoding = "utf-8" ) as file :
145
147
file .write (pretty_xml_str )
146
148
149
+
147
150
def build_xml_element (parent , data ):
148
- if isinstance (data , dict ):
149
- for key , value in data .items ():
150
- if isinstance (value , dict ):
151
- element = ET .SubElement (parent , key .replace (" " , "_" ))
152
- build_xml_element (element , value )
153
- elif isinstance (value , list ):
154
- for item in value :
155
- subelement = ET .SubElement (parent , key .replace (" " , "_" ))
156
- build_xml_element (subelement , item )
157
- else :
158
- element = ET .SubElement (parent , key .replace (" " , "_" ))
159
- element .text = str (value )
160
- else :
161
- parent .text = str (data )
151
+ if isinstance (data , dict ):
152
+ for key , value in data .items ():
153
+ if isinstance (value , dict ):
154
+ element = ET .SubElement (parent , key .replace (" " , "_" ))
155
+ build_xml_element (element , value )
156
+ elif isinstance (value , list ):
157
+ for item in value :
158
+ subelement = ET .SubElement (parent , key .replace (" " , "_" ))
159
+ build_xml_element (subelement , item )
160
+ else :
161
+ element = ET .SubElement (parent , key .replace (" " , "_" ))
162
+ element .text = str (value )
163
+ else :
164
+ parent .text = str (data )
165
+
162
166
163
167
def _parse_xml_element (element ):
164
- """
165
- Recursively convert an XML element to a dictionary.
166
-
167
- Args:
168
- element: The XML element.
169
-
170
- Returns:
171
- A dictionary representing the XML element and its children.
172
- """
173
- data = {}
174
- for child in element :
175
- if child .tag not in data :
176
- data [child .tag ] = []
177
- if len (child ) > 0 :
178
- data [child .tag ].append (_parse_xml_element (child ))
179
- else :
180
- data [child .tag ].append (child .text )
181
- for key , value in data .items ():
182
- if len (value ) == 1 :
183
- data [key ] = value [0 ]
184
- return data
168
+ """
169
+ Recursively convert an XML element to a dictionary.
170
+
171
+ Args:
172
+ element: The XML element.
173
+
174
+ Returns:
175
+ A dictionary representing the XML element and its children.
176
+ """
177
+ data = {}
178
+ for child in element :
179
+ if child .tag not in data :
180
+ data [child .tag ] = []
181
+ if len (child ) > 0 :
182
+ data [child .tag ].append (_parse_xml_element (child ))
183
+ else :
184
+ data [child .tag ].append (child .text )
185
+ for key , value in data .items ():
186
+ if len (value ) == 1 :
187
+ data [key ] = value [0 ]
188
+ return data
189
+
185
190
186
191
def ascii_encode_dict (data ):
187
192
ascii_encode = (
0 commit comments