Last active
January 25, 2024 12:15
-
-
Save arimariojesus/11cbc068a3adae7e54604e5174c0ab58 to your computer and use it in GitHub Desktop.
Decode a xml fetch response with ISO-8859-1 codification to UTF-8
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
const decodedResponse = await fetch('https://www.anysite.rss.xml', { | |
headers: { | |
'Content-Type': 'text/xml;charset=ISO-8859-1', | |
}, | |
}) | |
.then(r => r.arrayBuffer()) // resolve the response stream for buffer | |
.then(d => { | |
const dataView = new DataView(d); // creates a DataView object representing the buffer data | |
const isoDecoder = new TextDecoder('ISO-8859-1'); // creates an instance of the our decoder | |
const decodedString = isoDecoder.decode(dataView); // so we pass the stream of bytes for the decoder do its job | |
return new window.DOMParser().parseFromString(parsedString, 'text/xml'); // here we transform the string to html | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment