在解析XML提要时,用等效字符替换HTML实体(例如’)

use*_*025 2 javascript escaping special-characters titanium xml-parsing

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

政府已经为St Eunan学院的重大翻新项目提供资金.这是上个月宣布将其预制件替换为永久性住宿的补充.最新的补助金将允许对学校的一部分进行大规模整修,以便为课程提供新的住宿 - 该项目还将涉及屋顶维修,安装除尘系统,新的科学室配件和安装牢固的警报.Donegal代理Joe McHugh表示,学分必须归学校管理层所有

反正是否容易用例如撇号等替换这些特殊字符(即HTML实体)?

编辑:

Ti.API.info("is this real------------"+win.dataToPass)
Run Code Online (Sandbox Code Playgroud)


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

[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…….
Run Code Online (Sandbox Code Playgroud)


我的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);
Run Code Online (Sandbox Code Playgroud)

Jos*_*ter 5

有很多库可以包括钛(Underscore.string,string.js将做到这一点,但如果你只希望UNESCAPE HTML功能,只是试试这个代码,改编自上述库

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;
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

这将使用人类可读的衍生物替换这些特殊字符,并返回修改后的字符串.只需将它放在代码中的某个地方就可以了,我自己在Titanium中使用它并且非常方便.