Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

261
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!