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

265
Visualizações
How to append more than 1 nested node for XML in javascript?

Good day everyone,

I am currently trying to append a metadata file. Sorry in advance if I did anything wrong, I am unfamiliar with editing XML codes in JS.. Thanks! Currently, I am having difficulty getting the results that I expected. I am trying to insert 2 new nodes one nested over the other into the newParentTestNode.

I want to add a couple of nodes within the TestNode as seen in the results I want.. I can't seem to find a solution online. Please do help thanks!

I am currently getting this result:

<gmd:MTTEST><TESTNODE2/></gmd:MTTEST>

But the result I want is:

<gmd:MTTEST>
 <gmd:TestNode>
  <gmd:TestNode2>

  </gmd:TestNode2>
 </gmd:TestNode>
</gmd:MTTEST>

xmlTest: function (evt) { 
  if(this.item.metadata_standard_name == "Correct Data"){

        xmlString = this.item.sys_xml_clob;
       
        var metadataXmlString = jQuery.parseXML(xmlString); 

        let newParentTestNode = metadataXmlString.getElementsByTagName("gmd:MTTEST") 

        newNode = metadataXmlString.createElement("TestNode")
        newNode2 = metadataXmlString.createElement("TestNode2")

        let addMe = newNode.appendChild(newNode2)

        newParentTestNode[0].appendChild(addMe)        

        xmlString = (new XMLSerializer()).serializeToString(metadataXmlString);

        console.log(xmlString)
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

appendChild() returns the node that is appended not the parent node.

This means that newNode.appendChild(newNode2) returns newNode2, which you'll then append to your root node, effectively removing TestNode2 from TestNode and appending it directly to MTTEST.

You don't need to assign the result of appendChild to a new addMe variable because appendChild modifies the structure in-place, so you gain nothing from the return value (as you already have variables referencing both the parent and the child element). So in the end you just need to append newNode (which will already contain newNode2) to newParentTestNode.

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