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