You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-[Importing Related or Computed Fields](#importing-related-or-computed-fields)
@@ -1349,6 +1350,129 @@ my_mapping = {
1349
1350
...
1350
1351
}
1351
1352
```
1353
+
1354
+
#### XML Processing
1355
+
1356
+
The `XMLProcessor` class allows you to transform data from XML files into a format suitable for Odoo import, providing an alternative to the `Processor` class for XML-based data sources.
1357
+
1358
+
```python
1359
+
# -*- coding: utf-8 -*-
1360
+
from odoo_csv_tools.lib import mapper
1361
+
from odoo_csv_tools.lib.transform import Processor
`filename` (str): The path to the XML file to be processed.
1375
+
1376
+
`root_node_path` (str): An XPath expression specifying the root node(s) within the XML file to iterate over. Each node found by this XPath will be treated as a data record.
1377
+
1378
+
`conf_file` (str, optional): The path to a configuration file. Inherited from the Processor class but may not be used in the same way by XMLProcessor. Defaults to False.
Transforms data from the XML file based on the provided mapping.
1382
+
1383
+
*Args:*
1384
+
1385
+
`mapping` (dict): A dictionary that defines how data from the XML file should be mapped to fields in the output format (e.g., CSV). The keys of the dictionary are the target field names, and the values are XPath expressions to extract the corresponding data from the XML.
1386
+
1387
+
`filename_out` (str): The name of the output file where the transformed data will be written.
1388
+
1389
+
`import_args` (dict): A dictionary containing arguments that will be passed to the odoo_import_thread.py script (e.g., `{'model': 'res.partner', 'context': "{'tracking_disable': True}"}`).
1390
+
1391
+
`t (str, optional)`: This argument is kept for compatibility but is not used in XMLProcessor. Defaults to 'list'.
1392
+
1393
+
`null_values` (list, optional): This argument is kept for compatibility but is not used in XMLProcessor. Defaults to `['NULL', False]`.
1394
+
1395
+
`verbose` (bool, optional): This argument is kept for compatibility but is not used in XMLProcessor. Defaults to True.
1396
+
1397
+
`m2m (bool, optional)`: This argument is kept for compatibility but is not used in XMLProcessor. Defaults to False.
1398
+
1399
+
*Returns:*
1400
+
1401
+
`tuple`: A tuple containing the header (list of field names) and the transformed data (list of lists).
1402
+
1403
+
> **Important Notes:**
1404
+
The t, null_values, verbose, and m2m arguments are present for compatibility with the Processor class but are not actually used by the XMLProcessor.
1405
+
The mapping dictionary values should be XPath expressions that select the desired data from the XML nodes.
1406
+
1407
+
`XMLProcessor.split(split_fun)`
1408
+
Raises a NotImplementedError because the split functionality is not supported for XMLProcessor.
1409
+
1410
+
*Args:*
1411
+
1412
+
`split_fun`: This argument is not used.
1413
+
1414
+
*Raises:*
1415
+
1416
+
`NotImplementedError`: Indicates that the split method is not available for XML processing.
1417
+
1418
+
##### Example of XML to CSV Transformation
1419
+
1420
+
Let's say you have the following XML data:
1421
+
1422
+
```XML
1423
+
<?xml version="1.0"?>
1424
+
<data>
1425
+
<countryname="Liechtenstein">
1426
+
<rank>1</rank>
1427
+
<year>2008</year>
1428
+
<gdppc>141100</gdppc>
1429
+
<neighborname="Austria"direction="E"/>
1430
+
<neighborname="Switzerland"direction="W"/>
1431
+
</country>
1432
+
<countryname="Singapore">
1433
+
<rank>4</rank>
1434
+
<year>2011</year>
1435
+
<gdppc>59900</gdppc>
1436
+
<neighborname="Malaysia"direction="N"/>
1437
+
</country>
1438
+
<countryname="Panama">
1439
+
<rank>68</rank>
1440
+
<year>2011</year>
1441
+
<gdppc>13600</gdppc>
1442
+
<neighborname="Costa Rica"direction="W"/>
1443
+
<neighborname="Colombia"direction="E"/>
1444
+
</country>
1445
+
</data>
1446
+
```
1447
+
1448
+
To transform this into a CSV file with columns "name", "gdp", "year", and "neighbor", you would use the following Python script and mapping:
1449
+
1450
+
```Python
1451
+
from odoo_csv_tools.lib import mapper
1452
+
from odoo_csv_tools.lib.transform import XMLProcessor
A complete import project (transformation and load) is available in the repo [odoo_import_example](https://github.com/tfrancoi/odoo_import_example). It demonstrates use cases such as:
0 commit comments