Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong format after InsertBefore #258

Open
ognif opened this issue Nov 18, 2019 · 0 comments
Open

Wrong format after InsertBefore #258

ognif opened this issue Nov 18, 2019 · 0 comments

Comments

@ognif
Copy link

ognif commented Nov 18, 2019

If I add an element to my document, the final output is wrong formatted. The result is not valid I think.

Original XML

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

   <vars>
....

Changed XML

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

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

Expected XML

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

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

       <vars>

Code

var DOMParser = require('xmldom').DOMParser;
var serializer = new (require('xmldom')).XMLSerializer;

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);


            fs.writeFileSync(filePath,myFile,{encoding:'utf8',flag:'w+'});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant