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
_compute_hash_deduplicate(node, hash_maps)
¶
A function to compute hash for a document tree node and deduplicate its content
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node
|
tuple
|
A tuple of (node_type, content) representing a node |
required |
hash_maps
|
dict
|
A dict of dicts storing reference to deduplicated nodes keyed by their type and hash value |
required |
Returns:
Type | Description |
---|---|
tuple
|
A tuple of (node_type, content, hash) representing a node after deduplication |
Source code in xml2db/xml_converter.py
_parse_element_tree(xt)
¶
Parse an etree.ElementTree recursively
Parameters:
Name | Type | Description | Default |
---|---|---|---|
xt
|
ElementTree
|
an XML ElementTree object |
required |
Returns:
Type | Description |
---|---|
tuple
|
The parsed document tree (nested dict) |
Source code in xml2db/xml_converter.py
_parse_iterative(xml_file, recover=False)
¶
Parse an XML file into a document tree (nested dict) in an iterative fashion.
This method uses etree.iterparse and does not load the entire XML document in memory. It saves memory, especially if you decide to filter out nodes using 'document_tree_node_hook' hook.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
xml_file
|
Union[str, BytesIO]
|
an XML file to parse |
required |
recover
|
bool
|
should we try to parse incorrect XML? |
False
|
Returns:
Type | Description |
---|---|
tuple
|
A tuple of node_type, content (dict), hash |
Source code in xml2db/xml_converter.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
|
_parse_xml_node(node_type, node, compute_hash, hash_maps)
¶
Parse nodes of an XML document into a dict recursively
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node_type
|
str
|
type of the node to parse |
required |
node
|
Element
|
lxml node object |
required |
compute_hash
|
bool
|
should we compute hash and deduplicate? |
required |
hash_maps
|
dict
|
a dict referencing nodes based on their hash |
required |
Returns:
Type | Description |
---|---|
tuple
|
A tuple of node_type, content (dict), hash |
Source code in xml2db/xml_converter.py
_transform_node(node_type, content)
¶
Apply transformations to a given node
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node_type
|
str
|
The node type to transform |
required |
content
|
dict
|
The node content dict to transform |
required |
Returns:
Type | Description |
---|---|
tuple
|
A tuple of (node_type, content) for the transformed node |
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. |