Search code examples
javascriptjqueryxmlxml-parsingxmldom

How to create self enclosing element javascript XML DOM?


I want to create element like,

<Object name="object name" value="object value" />

but every time i end up with

<Object name="object name" value="object value"></Object>

I am using this code piece

parser = new DOMParser();
xmlDoc = parser.parseFromString(text, "text/xml");
newel = xmlDoc.createElement("Object");
x=xmlDoc.getElementsByTagName("Page")[0];
x.appendChild(newel);

Any suggestions?


Solution

  • The two representations of an empty XML element are perfectly equivalent. In the DOM representation, you have an element of name "Object" that does not have any (non-attribute) children. If you parse the two XML strings, you will get exactly the same DOM.

    The different textual representation depends on how you serialise your DOM tree into text.