0

I add a new node to an existing XML but the format is not well. Original XML is:

<xml xmlns:xi="http://www.w3.org/2001/XInclude">

   <vars>
....

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

<xml xmlns:xi="http://www.w3.org/2001/XInclude">

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

But I expect

<xml xmlns:xi="http://www.w3.org/2001/XInclude">

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

       <vars>

My code is simple:

var xmlDoc = new DOMParser().parseFromString(dataRegelWerk);
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";
                newEle.setAttributeNode(att1);
                newEle.setAttributeNode(att2);

                root.insertBefore(newEle, varselem);
            }

var myFile = serializer.serializeToString(xmlDoc);

Do I understand something wrong with insertBefore? or is this a good XML formatting and I just expect a wrong thing?

ingo
  • 776
  • 1
  • 10
  • 25
  • This may help https://stackoverflow.com/questions/376373/pretty-printing-xml-with-javascript – Lakshman Nov 16 '19 at 06:00
  • Thank you, but I do not think that I have to reformat an existing XML document. In my exes the Nodejs xmldim module has bugs – ingo Nov 18 '19 at 08:50

0 Answers0