2

當在解析XML飼料,我正在從內容的標記文字,就像這樣:替換HTML實體(例如’)與字符等效解析XML飼料

政府授予的資金用於重大翻新項目繼續在St Eunan ’ s學院進行。這是除了上個月’的宣佈,其預製件的授予將被永久性住宿所取代。最新的撥款將允許對學校的一部分進行大修,以允許爲類別–新的住宿項目還將涉及屋頂維修,安裝除塵系統,新科學室配件和安裝報警器。多尼戈爾副喬·麥克休說,信用證必須去學校’董事會管理的

反正有沒有輕鬆地與他們的性格等價物例如,省略號等替換這些特殊字符(即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); 
+0

避免在刪除它們?用它們的字符替換它們? - 你想用繩子做什麼? –

+0

@ Alex K.是的替換他們的字符等值。我將它們作爲文本顯示在窗口上 – user2363025

+0

@ Alex K.我意識到自定義查找和替換功能可以做到這一點,但我懷疑是否有另一種方式,因爲我必須知道所有可能出現的特殊字符 – user2363025

回答

5

有很多庫可以包括鈦(Underscore.stringstring.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; 
     } 
    }); 
} 

這將替換那些特殊字符與他們的人類可讀衍生物,並返回修改後的條款G。只要把它放在代碼的某個地方,你就可以走了,我已經在Titanium中使用了它,它非常方便。

+0

非常感謝您的幫助。我已經複製了這段代碼。爲了實際運行它,我嘗試過:var newText = unescape (win.datatoPass);其中win.datatoPass是一個字符串,變量newText是我設置的標籤的文本屬性,但它在我的應用中顯示爲undefined – user2363025

+0

我意識到,它應該是unescapeHTML (win.datatoPass)我現在已經嘗試過了:var newText = unescapeHTML(win.datatoPass);其中win.datatoPass是一個字符串,變量newText是我設置的標籤文本屬性,但是我的應用程序正在說不能調用方法'replace'of undefined,源代碼是'return str.replace(/ \ &([^;) +);/g,函數(entity,entityCode)'。我是否需要插入實體和實體代碼的值? – user2363025

+0

這意味着'win.datatoPass'是未定義的,請檢查'win.datatoPass'函數本身沒有問題。 –

0

以下是對這些特殊字符的兩個引用,不幸的是,通過將它們過濾出來,您可能會過濾出可能實際需要保留的重要信息。我的建議是使用符號參考表創建一個數組,然後在每個代碼的字符串中執行搜索,並用適當的響應替換代碼。

例如:

A-Z are represented by: &#65; to &#90;

過濾掉這些信息可能顯著改變你期望能夠讀取數據。

HTML符號實體參考:
http://www.webmonkey.com/2010/02/special_characters/
http://www.w3schools.com/tags/ref_symbols.asp

+0

@ user2363025您使用的編程語言可能支持包含執行我所描述的功能的搜索和替換例程的庫,這取決於您使用的語言以及您可能或不可以使用的庫機器。 –

+0

我正在使用鈦。任何想法,如果他們有這樣的庫 – user2363025

+0

存在兩種選擇。 1)構建一個自定義函數,用於搜索並重新編碼每個代碼。 2)它看起來像鈦是Java的衍生物,如果這是真的,那麼以下應該工作(儘管您可能需要引用Java標準庫,請訪問鈦頁面,瞭解如何創建該參考:http:// docs。 oracle.com/javase/6/docs/api/java/net/URLDecoder.html –

0

我遇到過同樣的問題,而@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; 
     }}); 
    } 
};