Skip to content

Instantly share code, notes, and snippets.

@JamesTheAwesomeDude
Last active February 21, 2023 15:30
Show Gist options
  • Save JamesTheAwesomeDude/a072e2378ddc34a52abae4f29a774a7c to your computer and use it in GitHub Desktop.
Save JamesTheAwesomeDude/a072e2378ddc34a52abae4f29a774a7c to your computer and use it in GitHub Desktop.
Firefox/Chrome DOM stdlib: create XML document from scratch, including declaration
// example: createXMLDocumentWithDeclaration('http://www.topografix.com/GPX/1/1', 'gpx', null, 'version="1.0" standalone="yes"')
function createXMLDocumentWithDeclaration(namespaceURI, qualifiedNameStr, documentType=null, declarationData='version="1.0"') {
const document = new Document().implementation.createDocument(namespaceURI, qualifiedNameStr, documentType);
// https://www.w3.org/TR/xml/#NT-XMLDecl
document.insertBefore(document.createProcessingInstruction("xml", declarationData), document.firstChild);
return document;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment