Home > Blockchain >  Json to XML with attributes rather than elements
Json to XML with attributes rather than elements

Time:10-10

I need to parse an Json like:

{
"operacion": "ingresarOrdenBilateral",
"agente" : "0062",
"comitente" : 7211,
"fechaOrigen" : "2021-09-23T16:51:27.873-03:00",
"tipo" : "V",
"instrumento" : "GD30",
"tipoVenc" : "24",
"precio" : "100000000",
"cantidad" : "1",
"idOrigen" : 10699570
"ejecucion" : "SINCRONICA"
}

To this XML:

<ingresarOrdenBilateral 
agente="150" idOrigen="10039" fechaOrigen="2018-08-16T11:28:08.495-03:00" tipo="V" 
instrumento="AA17" tipoVenc="24" cantidad="1000000" precio="1625" formaOp="C" 
ejecucion="SINCRONICA"/> 

I have tried the library xmltodict and dicttoxml but I can't manage to get XML using attributes rather than elements. Also I think that the XML format is not standard.

Thanks!

CodePudding user response:

Firstly, using attributes rather than child elements is completely standard for this kind of structure. It's perhaps a little less common than using child elements, but it's not really unusual and it's certainly not non-standard.

Secondly, standard off-the-shelf converters between JSON and XML never give you enough control to produce exactly the target structure that you want. If you want a particular XML format, then you have to be prepared to do a transformation on the result, which is usually very easy to achieve with XSLT.

If you use XSLT 3.0 then you can do the JSON-to-XML conversion and the subsequent transformation within a single stylesheet; but otherwise, using your favourite library converter followed by an XSLT (1.0 ) conversion is straightforward enough. You'll find plenty of examples of stylesheets that convert XML elements to attributes.

CodePudding user response:

If the XML is that simple the code below is able to do the job

data = {
"operacion": "ingresarOrdenBilateral",
"agente" : "0062",
"comitente" : 7211,
"fechaOrigen" : "2021-09-23T16:51:27.873-03:00",
"tipo" : "V",
"instrumento" : "GD30",
"tipoVenc" : "24",
"precio" : "100000000",
"cantidad" : "1",
"idOrigen" : 10699570,
"ejecucion" : "SINCRONICA"
}

xml = '<root>'   ' '.join(f'"{k}"="{v}"' for k,v in data.items())   '</root>'
print(xml)

output

<?xml version="1.0" encoding="UTF-8"?>
<root>"operacion"="ingresarOrdenBilateral" "agente"="0062" "comitente"="7211" "fechaOrigen"="2021-09-23T16:51:27.873-03:00" "tipo"="V" "instrumento"="GD30" "tipoVenc"="24" "precio"="100000000" "cantidad"="1" "idOrigen"="10699570" "ejecucion"="SINCRONICA"</root>

CodePudding user response:

Well, it can be done in one line using built-in xml.etree.ElementTree:

import xml.etree.ElementTree as ET

data = {
    "operacion": "ingresarOrdenBilateral",
    "agente": "0062",
    "comitente": 7211,
    "fechaOrigen": "2021-09-23T16:51:27.873-03:00",
    "tipo": "V",
    "instrumento": "GD30",
    "tipoVenc": "24",
    "precio": "100000000",
    "cantidad": "1",
    "idOrigen": 10699570,
    "ejecucion": "SINCRONICA"
}
ET.dump(ET.Element(data.pop("operacion"), {k: str(v) for k, v in data.items()}))

Output:

<ingresarOrdenBilateral agente="0062" comitente="7211" fechaOrigen="2021-09-23T16:51:27.873-03:00" tipo="V" instrumento="GD30" tipoVenc="24" precio="100000000" cantidad="1" idOrigen="10699570" ejecucion="SINCRONICA" />
  • Related