0

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?

2
  • as per w3c standards it automatically close the element, not self closed. there's no way to do... Commented Jul 11, 2014 at 6:47
  • Ok thanks, is there any workaround for this? other than string manipulation? Commented Jul 11, 2014 at 7:05

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

how can I serialize DOM tree with one option? I am using var xmlString = new XMLSerializer().serializeToString(xmlDoc);
I fear there is no way to customise the XMLSerializer to render empty elements as self-closing elements. If you really need that, the easiest way is probably to use string manipulation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.