@@ -120,16 +120,14 @@ def to_xml(self) -> str:
120
120
Convert the data dictionary to an XML string representation using the ElementTree library.
121
121
"""
122
122
from modelspec .utils import build_xml_element
123
+
123
124
root = ET .Element ("root" )
124
125
build_xml_element (root , self .to_dict ())
125
126
126
127
xml_string = ET .tostring (
127
- root ,
128
- encoding = 'utf-8' ,
129
- xml_declaration = False ,
130
- method = 'xml'
131
- ).decode ('utf-8' )
132
-
128
+ root , encoding = "utf-8" , xml_declaration = False , method = "xml"
129
+ ).decode ("utf-8" )
130
+
133
131
parsed_xml = xml .dom .minidom .parseString (xml_string )
134
132
pretty_xml = parsed_xml .toprettyxml (indent = " " * 4 )
135
133
return pretty_xml
@@ -219,7 +217,7 @@ def to_bson_file(self, filename: str, include_metadata: bool = True) -> str:
219
217
)
220
218
outfile .write (bson_data )
221
219
222
- return filename
220
+ return filename
223
221
224
222
def to_yaml (self , include_metadata : bool = True ) -> str :
225
223
"""
@@ -262,37 +260,38 @@ def to_yaml_file(
262
260
)
263
261
264
262
return filename
265
-
266
263
267
-
268
- def to_xml_file (self , filename : Optional [str ] = None , include_metadata : bool = True ) -> str :
264
+ def to_xml_file (
265
+ self ,
266
+ filename : Optional [str ] = None ,
267
+ include_metadata : bool = True ,
268
+ root = "modelspec" ,
269
+ ) -> str :
269
270
from modelspec .utils import build_xml_element
270
271
271
272
if filename is None :
272
273
filename = f"{ self .id } .xml"
273
274
274
- root = ET .Element (" root" ) # Create the root element
275
+ root = ET .Element (root )
275
276
276
277
build_xml_element (root , self .to_dict ())
277
278
278
279
# Create an ElementTree object with the root element
279
280
tree = ET .ElementTree (root )
280
281
281
282
# Generate the XML string
282
- xml_str = ET .tostring (root , encoding = "utf-8" ).decode ("utf-8" )
283
+ xml_str = ET .tostring (root , encoding = "utf-8" , method = "xml" ).decode ("utf-8" )
283
284
284
- # Pretty format the XML string using minidom
285
- #dom = xml.dom.minidom.parseString(xml_str)
286
- # pretty_xml_str = dom .toprettyxml(indent=" ")
285
+ # Create a pretty-formatted XML string using minidom
286
+ parsed_xml = xml .dom .minidom .parseString (xml_str )
287
+ pretty_xml_str = parsed_xml .toprettyxml (indent = " " * 4 )
287
288
288
- # Write the formatted XML data to the file
289
+ # Write the XML data to the file
289
290
with open (filename , "w" , encoding = "utf-8" ) as file :
290
- file .write (xml_str )
291
+ file .write (pretty_xml_str )
291
292
292
293
return filename
293
294
294
-
295
-
296
295
# def to_xml_file(self, filename: Optional[str] = None, include_metadata: bool = True) -> str:
297
296
# from modelspec.utils import build_xml_element
298
297
# if filename is None:
@@ -317,7 +316,6 @@ def to_xml_file(self, filename: Optional[str] = None, include_metadata: bool = T
317
316
318
317
# return filename
319
318
320
-
321
319
@classmethod
322
320
def from_file (cls , filename : str ) -> "Base" :
323
321
"""
@@ -390,7 +388,7 @@ def from_yaml_file(cls, filename: str) -> "Base":
390
388
d = yaml .safe_load (infile )
391
389
d = yaml_converter .structure (d , Dict )
392
390
return cls .from_dict (d )
393
-
391
+
394
392
@classmethod
395
393
def from_xml_file (cls , filename : str ) -> "Base" :
396
394
"""
0 commit comments