Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

154
Visualizações
Creating an XML response using node JS from JSON

I am looking to create an XML schema from an array in the object. The length of the array varies and based on that I would like to create a logic that generates the response. Below is the format and example.

Object - Exact

 "Data": [{
            "JK": true,
            "Number": "02154029",
            "Name": "Allen",
            "Type": "Delta",
            "Code": "ASM",
            "Amount": 10

        },
        {
            "JK": true,
            "Number": "92154429",
            "Name": "Peter",
            "Type": "Delta",
            "Code": "FLSM",
            "Amount": 50

        },
.
.
.
.
"n items"
]

Above is the object that varies its length, using the above object I would like to create the below XML schema.

XML - Expected

<request ...........>
  <delta.....>
    <lineItem id = 1....>
    </lineItem>
    <lineItem id = 2....>
    </lineItem>
    .
    .
    .
    <lineItem id = n....>(based on length of above array)
    </lineItem>
    <params amount = "10" code = "ASM" batch = "1">
    <params amount = "50" code = "FLSM" batch = "2">
    .
    .
    .
    .
    <params amount = n code = n batch = n>(based on length of above array)
 </delta>
 <keys number = "02154029" name = "Allen">
 <keys number= "92154429" name = "Peter">
 .
 .
 .
 <keys number= n name = n >(based on length of above array)
</request>

I request you to help me on this, by creating a logic using Javascript

Please let me know for any clarifications

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It's a little complicated, given your expected output, but doable, using a template, json parser, DOMParser and querySelector:

 tmpl = `<doc>
    <request>
         <delta>         
         </delta>         
         </request>
</doc>`

const data = `[{
            "JK": true,
            "Number": "02154029",
            "Name": "Allen",
            "Type": "Delta",
            "Code": "ASM",
            "Amount": 10

        },
        {
            "JK": true,
            "Number": "92154429",
            "Name": "Peter",
            "Type": "Delta",
            "Code": "FLSM",
            "Amount": 50

        }

]`
const jdata = JSON.parse(data);
const xmldoc = new DOMParser().parseFromString(
    tmpl,
    'text/xml'
  );
dest = xmldoc.querySelector('delta')

counter = 1;
for (let jd of jdata){
dest.insertAdjacentHTML(
      'beforeend',
      `<lineItem id = "${counter}"> </lineItem>
      `
    );
counter++;
}

for (let jd of jdata){
dest.insertAdjacentHTML(
      'beforeend',
      `<params amount = "${jd.Amount}" code = "${jd.Code}"> </params>
      
      `
    );
}
for (let jd of jdata){
dest.insertAdjacentHTML(
      'afterend',
      `
      <keys number = "${jd.Number}" name = "${jd.Name}"> </keys>
      `
    );
}


alert(xmldoc.documentElement.innerHTML)

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda