Stephanie Joyce
Stephanie Joyce

Reputation: 123

Angular 6 extract value from decoded string base 64

I've decoded a string in Base64 with xml2js library and i got this value :

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg width="293" height="102"
 viewBox="0 0 293 102"
 xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"  version="1.2" baseProfile="tiny">
<image 
width="293" 
height="102" 
xlink:href="data:image/png;base64,iVBORw0KGgoAAAANS......pASYAAAAASUVORK5CYII=" />
</svg>

i cut the encode value because it's to long

I'm blocked for getting the value of xlink:href, is there any technics or library for getting the attribute ?

I mean i want to get this value only : data:image/png;base64,iVBORw0KGgoAAAANS......pASYAAAAASUVORK5CYII=

Thank's

Upvotes: 1

Views: 267

Answers (1)

alexhughesking
alexhughesking

Reputation: 2007

You can use DOMParser to parse the xml string. Then you can operate on the resulting Document as usual:

const xmlString = `<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg width="293" height="102"
 viewBox="0 0 293 102"
 xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"  version="1.2" baseProfile="tiny">
<image 
width="293" 
height="102" 
xlink:href="data:image/png;base64,iVBORw0KGgoAAAANS......pASYAAAAASUVORK5CYII=" />
</svg>`;

const domParser = new DOMParser();
const xmlDoc = domParser.parseFromString(xmlString, 'application/xml');
const imageElement = xmlDoc.getElementsByTagName('image')[0];
const hrefAttr = imageElement.getAttribute('xlink:href');
console.log(hrefAttr);

Upvotes: 2

Related Questions