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?