Skip to content

Instantly share code, notes, and snippets.

@herdianf
Created December 7, 2023 10:11
Show Gist options
  • Save herdianf/ff396fe496fb887f9c487c6c821d3f0f to your computer and use it in GitHub Desktop.
Save herdianf/ff396fe496fb887f9c487c6c821d3f0f to your computer and use it in GitHub Desktop.
DOM XML Implementation
// Directly pass this as string
let xmlString = `<ern:NewReleaseMessage xmlns:ern="http://ddex.net/xml/ern/382"
MessageSchemaVersionId="ern/382"
LanguageAndScriptCode="en"
xs:schemaLocation="http://ddex.net/xml/ern/382 http://ddex.net/xml/ern/382/release-notification.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"></ern:NewReleaseMessage>`;
let parser = new DOMParser();
let xmlDoc = parser.parseFromString(xmlString, "text/xml");
// Or create programmatically
xmlDoc = document.implementation.createDocument(
"http://ddex.net/xml/ern/382",
"ern:NewReleaseMessage",
null,
);
xmlDoc.documentElement.setAttribute('MessageSchemaVersionId', 'ern/382');
xmlDoc.documentElement.setAttribute('LanguageAndScriptCode', 'en');
xmlDoc.documentElement.setAttributeNS('http://www.w3.org/2001/XMLSchema-instance',
'xs:schemaLocation',
'http://ddex.net/xml/ern/382 http://ddex.net/xml/ern/382/release-notification.xsd');
// Modify the dom
let el = xmlDoc.createElement('UpdateIndicator')
el.textContent = 'OriginalMessage'
xmlDoc.documentElement.appendChild(el)
// Serialize
let xmlResult = new XMLSerializer().serializeToString(xmlDoc);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment