0

I want to export some html as valid XML.

https://jsfiddle.net/xpvt214o/617391/

<div id="test">
  <p data-test="&nbsp">
  Foo&nbsp;bar
  </p>
</div>

The output of $('#test').html() is: <p data-test="&nbsp;"> Foo&nbsp;bar </p>. This is valid html. But the entity &nbsp; is not valid in XML. So a valid XML output would be <p data-test="&nbsp;"> Foo&#x00A0;bar </p>.

How can I create numerical entities/valid XML with $(...).html() ?

3

1 Answer 1

1

To get XML use XMLSerializer

var s = new XMLSerializer();
var d = document;
var str = s.serializeToString(d);
console.log(str)

This will return the current document to valid XML.

Working example here.

See here for more info on XMLSerializer.

Taken from this answer.

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

Comments

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.