Angular 6从解码的字符串库64中提取值

时间:2018-11-15 15:23:12

标签: angular typescript base64

我已经使用xml2js库在Base64中解码了一个字符串,并且得到了这个值:

<?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>

我切断了编码值,因为它太长了

我无法获取xlink:href的值,是否有任何技术或库来获取属性?

我的意思是我只想获得此值:data:image/png;base64,iVBORw0KGgoAAAANS......pASYAAAAASUVORK5CYII=

谢谢

1 个答案:

答案 0 :(得分:2)

您可以使用DOMParser来解析xml字符串。然后,您可以照常操作生成的文档:

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);