Ruwanka De Silva
Ruwanka De Silva

Reputation: 3755

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?

Upvotes: 0

Views: 372

Answers (1)

Michael
Michael

Reputation: 250

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.

Upvotes: 1

Related Questions