troubleshooting Question

Problem with XML format after insert a new XMLNode

Avatar of Ingo Förster
Ingo Förster asked on
1 Comment1 Solution111 ViewsLast Modified:
In my node app I have to insert a new XMLnode for identify it later. I developed some code where I expected the right result.
It is really simple:

var xmlDoc = new DOMParser().parseFromString(dataRegelWerk);  //Incomming data
if(allok == false){
                var root = xmlDoc.documentElement;

                var varselem = xmlDoc.getElementsByTagName('vars')[0];
                var newEle = xmlDoc.createElement('TCafe');
                var att1 = xmlDoc.createAttribute("initvalue");
                att1.value = "1";
                var att2 = xmlDoc.createAttribute("type");
                att2.value = "int";

                root.insertBefore(newEle, varselem);

var myFile = serializer.serializeToString(xmlDoc); //Put back to string to store in db

This is the original formatted XML

<xml xmlns:xi="">


I tried to insert before "vars" and the result is

<xml xmlns:xi="">

    <TCafe initvalue="1" type="int"/><vars>

But I expected

<xml xmlns:xi="">

    <TCafe initvalue="1" type="int"/>


Is there anything wrong with my code or do I expect a wrong thing? Maybe I have to close the new XMLNode? (I expected the last / will do it.
Any help will be great.
Senior Developer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 1 Comment.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros