Skip to content

Commit 2def615

Browse files
committed
Update Documentation with xml_processor
1 parent 5bbd9bb commit 2def615

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

README.md

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ It also provide a framework to manipulate data from CSV.
5252
- [Adding a column](#adding-a-column)
5353
- [Removing Lines](#removing-lines)
5454
- [Updating Records With Database IDs](#updating-records-with-database-ids)
55+
- [XML Processing](#XML-Processing)
5556
- [A Real Life Example](#a-real-life-example)
5657
- [Performances Considerations](#performances-considerations)
5758
- [Importing Related or Computed Fields](#importing-related-or-computed-fields)
@@ -1349,6 +1350,129 @@ my_mapping = {
13491350
...
13501351
}
13511352
```
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
1362+
from lxml import etree
1363+
1364+
processor = XMLProcessor(filename, root_node_path)
1365+
```
1366+
1367+
The XMLProcessor is initialized with the XML file to process and an XPath expression to identify the data records.
1368+
1369+
`XMLProcessor.__init__(filename, root_node_path, conf_file=False)`
1370+
Constructor for the XMLProcessor class.
1371+
1372+
*Args:*
1373+
1374+
`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.
1379+
1380+
`XMLProcessor.process(mapping, filename_out, import_args, t='list', null_values=['NULL', False], verbose=True, m2m=False)`
1381+
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+
<country name="Liechtenstein">
1426+
<rank>1</rank>
1427+
<year>2008</year>
1428+
<gdppc>141100</gdppc>
1429+
<neighbor name="Austria" direction="E"/>
1430+
<neighbor name="Switzerland" direction="W"/>
1431+
</country>
1432+
<country name="Singapore">
1433+
<rank>4</rank>
1434+
<year>2011</year>
1435+
<gdppc>59900</gdppc>
1436+
<neighbor name="Malaysia" direction="N"/>
1437+
</country>
1438+
<country name="Panama">
1439+
<rank>68</rank>
1440+
<year>2011</year>
1441+
<gdppc>13600</gdppc>
1442+
<neighbor name="Costa Rica" direction="W"/>
1443+
<neighbor name="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
1453+
1454+
processor = XMLProcessor('countries.xml', '/data/country')
1455+
1456+
mapping = {
1457+
'name': '/data/country/@name',
1458+
'gdp': '/data/country/gdppc/text()',
1459+
'year': '/data/country/year/text()',
1460+
'neighbor': '/data/country/neighbor/@name'
1461+
}
1462+
1463+
processor.process(mapping, 'countries.csv', {})
1464+
```
1465+
1466+
This would generate a CSV file like this:
1467+
1468+
```CSV
1469+
"name";"gdp";"year";"neighbor"
1470+
"Liechtenstein";"141100";"2008";"Austria"
1471+
"Singapore";"59900";"2011";"Malaysia"
1472+
"Panama";"13600";"2011";"Costa Rica"
1473+
```
1474+
1475+
13521476
## A Real Life Example
13531477
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:
13541478
- importing partners with multiple categories

0 commit comments

Comments
 (0)