2011-08-04 21 views
0

可以說我有與行的文本文件,例如:Protovis - 處理文本源

[4/20/11 17:07:12:875 CEST] 00000059 FfdcProvider W com.test.ws.ffdc.impl.FfdcProvider logIncident FFDC1003I: FFDC Incident emitted on D:/Prgs/testing/WebSphere/AppServer/profiles/ProcCtr01/logs/ffdc/server1_3d203d20_11.04.20_17.07.12.8755227341908890183253.txt com.test.testserver.management.cmdframework.CmdNotificationListener 134 
[4/20/11 17:07:27:609 CEST] 0000005d wle   E CWLLG2229E: An exception occurred in an EJB call. Error: Snapshot with ID Snapshot.8fdaaf3f-ce3f-426e-9347-3ac7e8a3863e not found. 
          com.lombardisoftware.core.TeamWorksException: Snapshot with ID Snapshot.8fdaaf3f-ce3f-426e-9347-3ac7e8a3863e not found. 
    at com.lombardisoftware.server.ejb.persistence.CommonDAO.assertNotNull(CommonDAO.java:70) 

反正是有能夠輕鬆地導入一個數據源,如到這個protovis,如果沒有什麼會最簡單的將其解析爲JSON格式的方法。例如,對於第一個條目可能解析像這樣:

[ 
    { 
"Date": "4/20/11 17:07:12:875 CEST", 
"Status": "00000059", 
"Msg": "FfdcProvider W com.test.ws.ffdc.impl.FfdcProvider logIncident FFDC1003I", 
}, 
] 

謝謝,大衛

回答

0

Protovis本身解析文本文件不提供任何公用設施,所以你的選擇是:

  • 使用Javascript將文本解析爲對象,很可能使用正則表達式。
  • 使用您選擇的文本解析語言或實用程序預處理文本,導出JSON文件。

你的選擇取決於以下幾個因素:

  • 數據是否有點靜態的,或者是你將要在新的或動態的文件中的每個你看它的時候運行呢?使用靜態數據,預處理可能最容易;與動態數據,這可能會增加一個惱人的額外步驟。

  • 你有多少數據?用JavaScript解析一個20K的文本文件是完全正確的;解析2MB文件的速度會非常慢,並且會在瀏覽器工作時導致瀏覽器掛起(除非您使用Workers)。

  • 如果涉及到大量的處理,您寧願將該負載放在服務器上(通過使用服務器端腳本進行預處理)還是放在客戶端上(通過在瀏覽器中執行)?

如果你想這樣做在Javascript中,根據您所提供的樣品,你可以做這樣的事情:

// Assumes var text = 'your text'; 
// use the utility of your choice to load your text file into the 
// variable (e.g. jQuery.get()), or just paste it in. 
var lines = text.split(/[\r\n\f]+/), 
    // regex to match your log entry beginning 
    patt = /^\[(\d\d?\/\d\d?\/\d\d? \d\d:\d\d:\d\d:\d{3} [A-Z]+)\] (\d{8})/, 
    items = [], 
    currentItem; 

// loop through the lines in the file 
lines.forEach(function(line) { 
    // look for the beginning of a log entry 
    var initialData = line.match(patt); 
    if (initialData) { 
     // start a new item, using the captured matches 
     currentItem = { 
      Date: initialData[1], 
      Status: initialData[2], 
      Msg: line.substr(initialData[0].length + 1) 
     } 
     items.push(currentItem); 
    } else { 
     // this is a continuation of the last item 
     currentItem.Msg += "\n" + line; 
    } 
}); 

// items now contains an array of objects with your data 
+0

感謝,我最終決定做服務器端 – David