Python module for converting XML DCC (https://www.ptb.de/dcc/) to JSON and vice versa
Project description
DCC XML<-->JSON Conversion
Thie Package contains an Libary for XML<-->JSON Conversion for the DCC (Digital Calibration Certificate). Witch contains an Python Module for the conversion. Aswell as an tornador REST-API Server to remote call this conversion.
Conversion Rules
The python Module xmlschema is used to convert the XML to JSON and vice versa. Since we want to convert xml fragments to json and vice versa, the libary performes template insertion if the conversion failed with the given data. The default position for template insertion is in a 'dcc:data' element with in the file minimalDCC_3_3_0_jinjaTemplate.xml
Installation
To use the conversion libary, install the package with pip:
pip install dccXMLJSONConv
to use the REST-API server install the package with the web option:
pip install dccXMLJSONConv .[web]
Usage of local conversion Libary:
#Imports
import json
from dccXMLJSONConv.dccConv import XMLToJson,JSONToXML
jsonDict=json.loads('COPY CONTENT FROM EXAMPLE ABOVE')
xmlSTr='COPY CONTENT FROM EXAMPLE ABOVE'
xmltoJsonDict=XMLToJson(xmlSTr)
jsontoXMLStr=JSONToXML(jsonDict)
Using the dcc_rest_server
- Install Additional requrenments , namly
fastapi~=0.70.0anduvicorn~=0.27.1from requirementsServer.txt
pip install -r requirementsServer.txt
- Start the dcc_server with
cd ~/repos/dcc_XMLJSONConv/src/dccXMLJSONConv
(venv) seeger01@n23017:~/repos/dccXMLJSONConv/src/dcc_XMLJSONConv$ uvicorn dccServer:app --reload
INFO: Will watch for changes in these directories: ['/home/seeger01/repos/dccXMLJSONConv/src/dccXMLJSONConv']
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [16614] using StatReload
INFO: Started server process [16616]
INFO: Waiting for application startup.
INFO: Application startup complete.
The REST-Server is now runing on Port 8000 use the --port and --host params to change port and host.
(venv) seeger01@n23017:~/repos/dccXMLJSONConv/src/dccXMLJSONConv$ uvicorn dccServer:app --reload --port 8001 --host 127.0.0.2
INFO: Will watch for changes in these directories: ['/home/seeger01/repos/dccXMLJSONConv/src/dccXMLJSONConv']
INFO: Uvicorn running on http://127.0.0.2:8001 (Press CTRL+C to quit)
REST API Documentation
This document outlines the REST API endpoints available in the application, detailing the request methods, endpoints, expected request bodies, and response formats.
Endpoints
| Endpoint | Method | Description | Request Body | Response |
|---|---|---|---|---|
/ |
GET | Returns a greeting message and usage instructions. | N/A | { "message": "<instructions>" } |
/dcc2json/ |
POST | Converts XML string input to JSON format. | { "xml": "<XML string>" } |
JSON object representing the original XML. |
/json2dcc/ |
POST | Converts JSON input to XML string format. | { "js": <JSON object> } |
XML string representing the original JSON. |
Details
/
- Method: GET
- Description: Provides a greeting message along with instructions for using the available endpoints.
- Request Body: None
- Response:
- Status Code: 200 OK
- Body:
{ "message": "Hello!\nBetter use the URL /json2dcc/?js={...} \n or /dcc2json (POST method)" }
/dcc2json/
- Method: POST
- Description: Accepts an XML string and converts it into a JSON representation.
- Request Body: Required. The body should contain an XML string within a JSON object under the key
"xml". - Response:
- Status Code: 200 OK for successful conversion; 400 Bad Request for empty input.
- Body: JSON object representing the parsed XML.
/json2dcc/
- Method: POST
- Description: Accepts a JSON object and converts it into an XML string.
- Request Body: Required. The body should contain a JSON object under the key
"js". - Response:
- Status Code: 200 OK for successful conversion; 400 Bad Request for empty input.
- Body: XML string representing the JSON object.
Further usage examples
For detailed usage examples, including request and response examples, refer to the automated tests provided intest_dccXMLJSONConv.py and test_dcc_server.py .
Update repeatable Fieldnames from schema URL
The skript [listTypeFinder.py][src/dcc_XMLJSONConv/listTypeFinder.py] can be used to update the list of repeated fields. Either with the functions in the skript (see Docstings for further Information) or calling the script with the XSD Url and optinal the output path like
(venv) user@host:~/repos/dccXMLJSONConv/src/dccXMLJSONConv$ python3 ./listTypeFinder.py https://ptb.de/dcc/v3.2.1/dcc.xsd tmp.json
List types have been written to tmp.json
dcc_rest_server debugging and launching with Pycharm
For Pycharm debuigging see : [https://www.jetbrains.com/help/pycharm/fastapi-project.html#run-debug-configuration}(https://www.jetbrains.com/help/pycharm/fastapi-project.html#run-debug-configuration) Create new fastApi launch configuration instead of python
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dccxmljsonconv-3.0.0.dev8.tar.gz.
File metadata
- Download URL: dccxmljsonconv-3.0.0.dev8.tar.gz
- Upload date:
- Size: 40.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6dc10e2c83d379cda86a4f41d0a428965059077a8680b84622ba9816ceb01fe0
|
|
| MD5 |
529559e693d1bdd00c03d68daca04d66
|
|
| BLAKE2b-256 |
7301213869bc5056bcc6126d4156dcd3ec556c079e12c147d9f6eb8c039a27c6
|
File details
Details for the file dccxmljsonconv-3.0.0.dev8-py3-none-any.whl.
File metadata
- Download URL: dccxmljsonconv-3.0.0.dev8-py3-none-any.whl
- Upload date:
- Size: 40.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
506a9b65e046807d0799e2725645c557d3fcfbebaa78fb3b47aa49a87feb2c49
|
|
| MD5 |
3fa089b16ab4d8ad99ccad9b10869cfe
|
|
| BLAKE2b-256 |
19f0bdbccdf2d59d48aa3ed1c0941f9fc64df4957313c8f1d281e895c64ee567
|