In my javascript I have the following:
let person = document.createElement('person');
let name = document.createElement('name');
let surname = document.createElement('surname');
person.appendChild(name);
person.appendChild(surname);
let xml = person;
How do I save my "xml" variable in a file (using javascript only)? OBS: The content should not be presented in a single line, but in the tree structure:
<person>
<name></name>
<surname></surname>
</person>
The XML Document Object Model (DOM) class is an in-memory representation of an XML document. The DOM allows you to programmatically read, manipulate, and modify an XML document.
Simply click the File button (the 3 lines), and click Save Page As. For example, I went to xml-sitemaps.com/sitemap.xml and clicked Save Page As. It saved as XML to my local machine and loaded as such. Without any HTML.
There's a very simple way to serialize your document to XML using XMLSerializer
.
Here is the process:
xhtml
namespace using String.prototype.replace
let person = document.createElement('person');
let name = document.createElement('name');
let surname = document.createElement('surname');
person.appendChild(name);
person.appendChild(surname);
// 1.) use XMLSerializer
let xml = new XMLSerializer().serializeToString(person);
// 2.) remove xml namespace
let xmlWithoutNamespace = xml.replace(' xmlns="http://www.w3.org/1999/xhtml"', '');
// 3.) use vkbeautify or your other favorite library to pretty print
console.log(vkbeautify.xml(xmlWithoutNamespace));
<script src="https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/vkbeautify/vkbeautify.0.99.00.beta.js"></script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With