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