Do XMLSerializer not collapse empty tags?

Is it possible with any configuration option to tell JavaScript XMLSerializer not to collapse empty tags into self-closing tags? I feed the xml string to the xml parser and then reserialize it after making changes to the tree, but where I have an explicit closing tag in the input, it collapses into a self-closing tag as a result, which causes problems.

+4
source share
2 answers

Well, I didn’t find anything, but since I used jQuery, I used this:

 $('<div>').append( $(mynode).clone() ).html(); 

instead

 new XMLSerializer().serializeToString(mynode); 

There were no self-closing tags in the jQuery version.

Note. My problem was only with IE11, so I used:

 if (Object.hasOwnProperty.call(window, "ActiveXObject") && !window.ActiveXObject && window.jQuery) { // is IE11 && Jquery $('<div>').append( $(newnode).clone() ).html(); } else { oldnode.outerHTML = new XMLSerializer().serializeToString(newnode); } 
0
source

If you don't have jquery and only need to go through the xmlserialzer, you can also use the npm package 'xmldom' .

 var serializer = require('xmldom').XMLSerializer; var str = serializer.serializeToString('your node'); 

It takes care of closing tags (e.g. script tags). NPM xmldom Works great in IE11.

0
source

All Articles