Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 解析XML提要时,将HTML实体(例如&;8217;)替换为字符等价物_Javascript_Xml Parsing_Escaping_Titanium_Special Characters - Fatal编程技术网

Javascript 解析XML提要时,将HTML实体(例如&;8217;)替换为字符等价物

Javascript 解析XML提要时,将HTML实体(例如&;8217;)替换为字符等价物,javascript,xml-parsing,escaping,titanium,special-characters,Javascript,Xml Parsing,Escaping,Titanium,Special Characters,解析XML提要时,我从内容标记获取文本,如下所示: 政府已经为圣尤南学院的一个大型翻新项目拨款。除此之外,上个月还宣布批准将其预制房屋更换为永久性住宿。最新的拨款将允许对学校的一部分进行重大整修,以便为班级提供新的住宿——该项目还将涉及屋顶维修、安装除尘系统、新的科学室配件和安装牢固的报警器。Donegal副校长Joe McHugh说,学分必须归学校管理委员会所有 是否有任何方法可以轻松地将这些特殊字符(即HTML实体)(例如撇号等)替换为它们的等效字符 编辑: 返回:(为清晰起见添加了换行符

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

政府已经为圣尤南学院的一个大型翻新项目拨款。除此之外,上个月还宣布批准将其预制房屋更换为永久性住宿。最新的拨款将允许对学校的一部分进行重大整修,以便为班级提供新的住宿——该项目还将涉及屋顶维修、安装除尘系统、新的科学室配件和安装牢固的报警器。Donegal副校长Joe McHugh说,学分必须归学校管理委员会所有

是否有任何方法可以轻松地将这些特殊字符(即HTML实体)(例如撇号等)替换为它们的等效字符

编辑:


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


下面是我的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);
var-win=Titanium.UI.currentWindow;
Ti.API.info(“这是真的吗?”+win.dataToPass);
var-escapeChars={lt:'',quot:'',apos:'',amp:'&'};
函数unescapeHTML(str){//由下划线.string和string.js修改而来
返回str.replace(/\&([^;]+);/g,函数(实体,entityCode){
var匹配;
if(escapeChars中的entityCode){
返回转义码[entityCode];
}else if(match=entityCode.match(/^#x([\da-fA-F]+)$/){
返回字符串.fromCharCode(parseInt(匹配[1],16));
}else if(match=entityCode.match(/^#(\d+)$/){
返回字符串.fromCharCode(~~匹配[1]);
}否则{
返回实体;
}
});
}
var newText=unescapethtml(win.datatoPass);
var label=Titanium.UI.createLabel({
颜色:“黑色”,
//text:win.dataToPass,//这很有效!
text:newText,//这会导致错误
字体:“Helvetica”,
尺寸:50,
宽度:“自动”,
高度:“自动”,
textAlign:“居中”
})
添加(标签);

下面是对这些特殊字符的两个引用,不幸的是,通过过滤掉它们,您可能会过滤掉您实际想要保留的重要信息。我的建议是使用符号引用表创建一个数组,然后在字符串中搜索每一个代码,并用相应的代码替换代码e响应。

例如: A-Z表示为:A;到Z;
过滤掉这些信息可能会显著更改您希望读取的数据。

HTML符号实体引用:


Tianium()中有许多库可以包含,这将实现这一点,但如果您只需要函数,请尝试从上述库改编的代码

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-escapeChars={lt:'',quot:'',apos:'',amp:'&'};
函数unescapeHTML(str){//由下划线.string和string.js修改而来
返回str.replace(/\&([^;]+);/g,函数(实体,entityCode){
var匹配;
if(escapeChars中的entityCode){
返回转义码[entityCode];
}else if(match=entityCode.match(/^#x([\da-fA-F]+)$/){
返回字符串.fromCharCode(parseInt(匹配[1],16));
}else if(match=entityCode.match(/^#(\d+)$/){
返回字符串.fromCharCode(~~匹配[1]);
}否则{
返回实体;
}
});
}

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

我遇到了同样的问题,@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;
        }});
    }
};
this.unescapethtml=函数(str){
var-escapeChars={lt:'',quot:'',apos:'',amp:'&'};
if(typeof(str)!='string'){
返回str;
}否则{
返回str.replace(/\&([^;]+);/g,函数(实体,entityCode){
var匹配;
if(escapeChars中的entityCode){
返回转义码[entityCode];
}else if(match=entityCode.match(/^#x([\da-fA-F]+)$/){
返回字符串.fromCharCode(parseInt(匹配[1],16));
}else if(match=entityCode.match(/^#(\d+)$/){
返回字符串.fromCharCode(~~匹配[1]);
}否则{
返回实体;
}});
}
};

避免删除它们?用它们的等效字符替换它们?-你想对字符串做什么?@Alex K.是的,用它们的等效字符替换它们。我在窗口上以文本形式显示它们@Alex K.我意识到自定义的查找和替换函数可以做到这一点,但我想知道是否还有其他方法可以做到知道浏览器中可能出现的所有特殊字符吗?然后可以使用模式匹配或通过设置dom成员html来读取其节点文本;@AlexK。不,它不在browser@user2363025您使用的编程语言可能支持包含执行以下操作的搜索和替换例程的库:我描述的函数,这将取决于您使用的语言以及您在机器上可能有或没有的库。我使用的是Tianium。知道他们是否有这样的库吗?存在两个选项。1)构建一个自定义函数,搜索并重新计算每个代码。2)看起来Tianium是Java的衍生产品,如果这是真的,那么下面应该可以工作(尽管您可能必须引用java标准库,但请访问Tianium页面以了解如何创建该引用:非常感谢您的帮助。我已将此代码复制到中。为了实际运行它,我尝试了:var newText=unescape(win.datatoPass);其中win.datatoPass是一个字符串。变量newText是我随后设置为文本p的内容
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;
        }
    });
}
    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;
        }});
    }
};