Skip to content

Commit 351c5de

Browse files
fixed issue parsing issue
1 parent 4340f45 commit 351c5de

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

src/modelspec/base_types.py

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,14 @@ def to_xml(self) -> str:
120120
Convert the data dictionary to an XML string representation using the ElementTree library.
121121
"""
122122
from modelspec.utils import build_xml_element
123+
123124
root = ET.Element("root")
124125
build_xml_element(root, self.to_dict())
125126

126127
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+
133131
parsed_xml = xml.dom.minidom.parseString(xml_string)
134132
pretty_xml = parsed_xml.toprettyxml(indent=" " * 4)
135133
return pretty_xml
@@ -219,7 +217,7 @@ def to_bson_file(self, filename: str, include_metadata: bool = True) -> str:
219217
)
220218
outfile.write(bson_data)
221219

222-
return filename
220+
return filename
223221

224222
def to_yaml(self, include_metadata: bool = True) -> str:
225223
"""
@@ -262,37 +260,38 @@ def to_yaml_file(
262260
)
263261

264262
return filename
265-
266263

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:
269270
from modelspec.utils import build_xml_element
270271

271272
if filename is None:
272273
filename = f"{self.id}.xml"
273274

274-
root = ET.Element("root") # Create the root element
275+
root = ET.Element(root)
275276

276277
build_xml_element(root, self.to_dict())
277278

278279
# Create an ElementTree object with the root element
279280
tree = ET.ElementTree(root)
280281

281282
# 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")
283284

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)
287288

288-
# Write the formatted XML data to the file
289+
# Write the XML data to the file
289290
with open(filename, "w", encoding="utf-8") as file:
290-
file.write(xml_str)
291+
file.write(pretty_xml_str)
291292

292293
return filename
293294

294-
295-
296295
# def to_xml_file(self, filename: Optional[str] = None, include_metadata: bool = True) -> str:
297296
# from modelspec.utils import build_xml_element
298297
# if filename is None:
@@ -317,7 +316,6 @@ def to_xml_file(self, filename: Optional[str] = None, include_metadata: bool = T
317316

318317
# return filename
319318

320-
321319
@classmethod
322320
def from_file(cls, filename: str) -> "Base":
323321
"""
@@ -390,7 +388,7 @@ def from_yaml_file(cls, filename: str) -> "Base":
390388
d = yaml.safe_load(infile)
391389
d = yaml_converter.structure(d, Dict)
392390
return cls.from_dict(d)
393-
391+
394392
@classmethod
395393
def from_xml_file(cls, filename: str) -> "Base":
396394
"""

0 commit comments

Comments
 (0)