Michael Wasser
Michael Wasser

Reputation: 1837

Javascript created xml document formatting of empty elements

Is it possible to control how an xml document is rendered by XMLSerializer when using your own namespace? Namely, when creating a document like this:

root = document.implementation.createDocument('hello-world', 'Something', null);
s = new XMLSerializer();
console.log(s.serializeToString(root));

The resulting xml from serializeToString is

<Something xmlns="hello-world"/>

Is there any way to change the formatting such that the output is instead

<Something xmlns="hello-world"></Something>

Upvotes: 1

Views: 96

Answers (1)

Bergi
Bergi

Reputation: 664936

It worked for me to add an empty text node:

root.documentElement.appendChild(root.createTextNode(""));

With that line included, I get the output

<?xml version="1.0"?><Something xmlns="hello-world"></Something>

Upvotes: 1

Related Questions