gpt4 book ai didi

javascript - 气体 : parse XML - decode HTML entity name fails - decode entity decimal code succeeds

转载 作者:行者123 更新时间:2023-11-30 00:27:40 26 4
gpt4 key购买 nike

使用 Google Apps 脚本,我想解码 HTML,例如:

Some text &#x26; text <br/> &cent;

存储为:

Some text & text 
¢

所以,类似的问题:How to decode HTML entities

发布为新问题,因为答案在使用 HTML 实体名称时不起作用,并且支持的 GAS 服务已发生变化。

我使用:

var str = 'Some text &#x26; text <br/> &cent;';
var xml = XmlService.parse('<d>' + str + '</d>');
var strDecoded = xml.getRootElement().getText();
Logger.log(strDecoded);

解析时GAS报错信息:

TypeError: The entity "cent" was referenced, but not declared.

我正在使用 &cent;例如,我测试了其他几个 HTML entity names , 结果都一样。

当我使用实体十进制代码而不是 HTML entity name 时它工作正常(在这种情况下: &#162; 而不是 &cent; )。与旧 GAS 服务效果相同。

任何可以在 GAS 中解析上述 HTML 的解决方案都将受到赞赏。

最佳答案

老问题,但我设法用这种方式解决了

function cleanHTML(html){
var decoded = '';
var xml = XmlService
.parse('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html>' + html + '</html>')
.getRootElement()
.getChildren().forEach(function(el){
decoded+=el.getValue();
});
//Logger.log(decoded)
return decoded
}

关于javascript - 气体 : parse XML - decode HTML entity name fails - decode entity decimal code succeeds,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30846935/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com