4

XML フィードを解析すると、コンテンツ タグから次のようなテキストが取得されます。

政府は、セント ユーナンズ カレッジを進める大規模な改修プロジェクトに資金を提供しました。これは、プレハブを恒久的な宿泊施設に置き換えるという先月の発表に加えてのことです。最新の助成金により、学校の一部の大規模な改修が可能になり、クラス用の新しい宿泊施設ができるようになります。このプロジェクトには、屋根の修理、集塵システムの設置、新しい科学室の備品、固定アラームの設置も含まれます。ドニゴール副ジョー・マクヒューは、クレジットは学校の管理委員会に送られなければならないと言います

アポストロフィなどのこれらの特殊文字 (つまり、HTML エンティティ) を同等の文字に簡単に置き換える方法はありますか?

編集:

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

3 に答える 3

1

私は同じ問題に遭遇しました.@ Josiah Hesterの解決策はうまくいきます. 文字列値のみが処理されることを確認する条件を追加しました。

    this.unescapeHTML = function(str) {
    var escapeChars = { lt: '<', gt: '>', quot: '"', apos: "'", amp: '&' };
    if(typeof(str) !== 'string'){
        return str;
    }else{
        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;
        }});
    }
};
于 2015-07-17T16:28:38.500 に答える