XMLConverter¶
A class to convert data from document tree format (nested dict) to and from XML.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_model |
DataModel
|
The |
required |
document_tree |
dict
|
Data in the document tree format (optional, can be built later by the |
None
|
Source code in xml2db/xml_converter.py
parse_xml(xml_file, file_path=None, skip_validation=False, recover=False, iterparse=True)
¶
Parse an XML document into a nested dict and performs the simplifications defined in the DataModel object ("pull" child to upper level, transform a choice model into "type" and "value" fields or concatenate children as string).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
xml_file |
Union[str, BytesIO]
|
An XML file path or file content to be converted |
required |
file_path |
str
|
The file path to be printed in logs |
None
|
skip_validation |
bool
|
Whether we should validate XML against the schema before parsing |
False
|
recover |
bool
|
Try to process malformed XML (lxml option) |
False
|
iterparse |
bool
|
Parse XML using iterative parsing, which is a bit slower but uses less memory |
True
|
Returns:
Type | Description |
---|---|
tuple
|
The parsed data in the document tree format (nested dict) |
Source code in xml2db/xml_converter.py
to_xml(out_file=None, nsmap=None, indent=' ')
¶
Convert a document tree (nested dict) into an XML file
Parameters:
Name | Type | Description | Default |
---|---|---|---|
out_file |
str
|
If provided, write output to a file. |
None
|
nsmap |
dict
|
An optional namespace mapping. |
None
|
indent |
str
|
A string used as indentin XML output. |
' '
|
Returns:
Type | Description |
---|---|
Element
|
The etree object corresponding to the root XML node. |