Created
December 7, 2023 10:11
-
-
Save herdianf/ff396fe496fb887f9c487c6c821d3f0f to your computer and use it in GitHub Desktop.
DOM XML Implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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