解析 XML 提要时,将 HTML 实体(例如 ')替换为等效字符

2023-12-27

解析 XML 提要时,我从内容标记获取文本,如下所示:

政府已为圣尤南学院的一项重大整修项目提供资金。这是继上个月宣布拨款将其预制房屋更换为永久住宿之后的补充。最新的拨款将用于对学校的一个部分进行重大整修,以便为班级提供新的住宿——该项目还将包括屋顶维修、安装除尘系统、新的科学室配件和安装坚固的警报器。多尼戈尔副警长乔·麦克休表示,这必须归功于学校管理委员会

无论如何,是否可以轻松地将这些特殊字符(即 HTML 实体)替换为它们的等效字符?

EDIT:

Ti.API.info("is this real------------"+win.dataToPass)


返回:(为了清晰起见添加换行符)

[INFO][TiAPI   ( 5437)]  Is this real------------------Police in Strabane are
warning home owners and car owners in the town to be vigilant following a recent
spate of break-ins. There has been a number of thefts from gardens and vehicles
in the Jefferson Court and Carricklynn Avenue area of the town. The PSNI have
said that residents have reported seeing a dark haired male in and around the
area in the early hours of the morning. Local Cllr Karina Carlin has been
monitoring the situation – she says the problem seems to be getting
worse…….


我的 external.js 文件位于下面,即仅显示上面文本的文件:

var win= Titanium.UI.currentWindow;

Ti.API.info("Is this real------------------"+ win.dataToPass);

var escapeChars = { lt: '<', gt: '>', quot: '"', apos: "'", amp: '&' };

function unescapeHTML(str) {//modified from underscore.string and string.js
    return str.replace(/\&([^;]+);/g, function(entity, entityCode) {
        var match;

        if ( entityCode in escapeChars) {
            return escapeChars[entityCode];
        } else if ( match = entityCode.match(/^#x([\da-fA-F]+)$/)) {
            return String.fromCharCode(parseInt(match[1], 16));
        } else if ( match = entityCode.match(/^#(\d+)$/)) {
            return String.fromCharCode(~~match[1]);
        } else {
            return entity;
        }
    });
}

var newText= unescapeHTML(win.datatoPass);


var label= Titanium.UI.createLabel({
    color: "black",
    //text: win.dataToPass,//this works!
    text:newText,//this is causing an error
    font: "Helvetica",
    fontSize: 50,
    width: "auto",
    height: "auto",
    textAlign: "center"
})

win.add(label);

您可以在 Titanium 中包含许多库(下划线.string http://epeli.github.io/underscore.string/, 字符串.js http://stringjs.com这将使这一切发生,但如果你只想取消转义 html http://stringjs.com/#methods/unescapehtml函数,只需尝试这段代码,改编自上述库

var escapeChars = { lt: '<', gt: '>', quot: '"', apos: "'", amp: '&' };

function unescapeHTML(str) {//modified from underscore.string and string.js
    return str.replace(/\&([^;]+);/g, function(entity, entityCode) {
        var match;

        if ( entityCode in escapeChars) {
            return escapeChars[entityCode];
        } else if ( match = entityCode.match(/^#x([\da-fA-F]+)$/)) {
            return String.fromCharCode(parseInt(match[1], 16));
        } else if ( match = entityCode.match(/^#(\d+)$/)) {
            return String.fromCharCode(~~match[1]);
        } else {
            return entity;
        }
    });
}

这会将这些特殊字符替换为其人类可读的派生字符,并返回修改后的字符串。只需将其放在代码中的某个位置即可,我自己在 Titanium 中使用过它,它非常方便。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

解析 XML 提要时,将 HTML 实体(例如 ')替换为等效字符 的相关文章

随机推荐