1
我通過Ajax接收響應的身體得到請求,並在該機構我感興趣的一個特定部分,看起來像這樣:解析HTML響應體字符串提取部分
if (w2.isHidden() == false) {
var counterValue = 0;
try {
var url = site + '/user.widget?page=just_data&query=Select IFNULL(time(MAX(longestWaitingSeconds), "unixepoch") , time(0, "unixepoch")) From AvayaCMSAT_skill';
var loader = dhtmlxAjax.getSync(url);
var text = loader.xmlDoc.responseText;
var dataCells = text.split("<cell>");
if (dataCells.length >= 2) {
counterValue = dataCells[1];
counterValue = counterValue.substring(0, counterValue.indexOf("<"));
}
} catch (err) {}
var elem = document.getElementById("counter2_Value");
elem.innerHTML = counterValue;
}
if (w5.isHidden() == false) {
var counterValue = 0;
try {
var url = site + '/user.widget?page=just_data&query=Select SUM(callsWaiting) From AvayaCMSAT_skill';
var loader = dhtmlxAjax.getSync(url);
var text = loader.xmlDoc.responseText;
var dataCells = text.split("<cell>");
if (dataCells.length >= 2) {
counterValue = dataCells[1];
counterValue = counterValue.substring(0, counterValue.indexOf("<"));
}
} catch (err) {}
var elem = document.getElementById("counter5_Value");
elem.innerHTML = counterValue;
}
if (w10.isHidden() == false) {
var counterValue = 0;
try {
var url = site + '/user.widget?page=just_data&query=Select SUM(callsWaiting) From AvayaCMSAT_vdn';
var loader = dhtmlxAjax.getSync(url);
var text = loader.xmlDoc.responseText;
var dataCells = text.split("<cell>");
if (dataCells.length >= 2) {
counterValue = dataCells[1];
counterValue = counterValue.substring(0, counterValue.indexOf("<"));
}
} catch (err) {}
var elem = document.getElementById("counter10_Value");
elem.innerHTML = counterValue;
}
if (w11.isHidden() == false) {
var counterValue = 0;
try {
var url = site + '/user.widget?page=just_data&query=Select IFNULL(time(MAX(longestWaitingSeconds), "unixepoch") , time(0, "unixepoch")) From AvayaCMSAT_vdn';
var loader = dhtmlxAjax.getSync(url);
var text = loader.xmlDoc.responseText;
var dataCells = text.split("<cell>");
if (dataCells.length >= 2) {
counterValue = dataCells[1];
counterValue = counterValue.substring(0, counterValue.indexOf("<"));
}
} catch (err) {}
var elem = document.getElementById("counter11_Value");
elem.innerHTML = counterValue;
}
if (w1.isHidden() == false) {
try {
var windowDims = getWindowStatus(1, 'grid');
var url = site + '/user.widget?page=just_data&query=Select groupName as "Group Name ", groupNumber as "Group Number", callsWaiting as " Calls Waiting ", longestWaiting as "Longest Waiting Call ", acceptableServiceLevel as "Acceptable Service Level", percentInServiceLevel as " Percent Within Service Level", staffed as "Staffed Agents ", available as " Agents Available" From AvayaCMSAT_skill&windowWidth=' + windowDims[2];
var loader = dhtmlxAjax.getSync(url);
if (loader.xmlDoc.responseText.length > 0) {
grid1.parse(loader.xmlDoc.responseText, 'xml');
grid1.loadSortingFromCookie('grid1');
grid1.loadSizeFromCookie('grid1');
}
} catch (err) {}
}
if (w6.isHidden() == false) {
try {
var url = site + '/user.widget?page=just_chart_data&query=SELECT SUM(available) as "Staffed Agents", SUM(staffed - available) as "Unavailable Agents" from AvayaCMSAT_skill&widgetType=pie_medium';
var loader = dhtmlxAjax.getSync(url);
pie_medium6.clearAll();
pie_medium6.parse(loader.xmlDoc.responseText, 'xml');
pie_medium6.refresh();
} catch (err) {}
}
if (w7.isHidden() == false) {
try {
var windowDims = getWindowStatus(7, 'grid');
var url = site + '/user.widget?page=just_data&query=Select agentName as "Agent Name", loginId as "LOGIN ID", agentExt as "Agent Extension", groupName as "Group Name", groupNumber as "Group Number", state as "Agent State", IFNULL(time((statusDuration), "unixepoch") , time(0, "unixepoch")) as"Status Duration" From AvayaCMSAT_agent&windowWidth=' + windowDims[2];
var loader = dhtmlxAjax.getSync(url);
if (loader.xmlDoc.responseText.length > 0) {
grid7.parse(loader.xmlDoc.responseText, 'xml');
grid7.loadSortingFromCookie('grid7');
grid7.loadSizeFromCookie('grid7');
}
}
catch (err) {}
}
if (w8.isHidden() == false) {
try {
var windowDims = getWindowStatus(8, 'grid');
var url = site + '/user.widget?page=just_data&query=Select groupName as "Group Name", groupNumber as "Group Number", SUM(acdCalls) as " Acd Calls ", SUM(extInCalls) as " Extension In Calls ", SUM(extOutCalls) as " Extension Out Calls" From AvayaCMSAT_agent GROUP BY groupName, groupNumber&windowWidth=' + windowDims[2];
var loader = dhtmlxAjax.getSync(url);
if (loader.xmlDoc.responseText.length > 0) {
grid8.parse(loader.xmlDoc.responseText, 'xml');
grid8.loadSortingFromCookie('grid8');
grid8.loadSizeFromCookie('grid8');
}
}
catch (err) {}
}
if (w9.isHidden() == false) {
try {
var windowDims = getWindowStatus(9, 'grid');
var url = site + '/user.widget?page=just_data&query=Select agentName as "Agent Name", loginId as "LOGIN ID", groupName as "Group Name", groupNumber as "Group Number", totalAcdCalls as "Total ACD Calls", avgTalkTime as "Average Talk Time", totalAfterCall as "Total After Call", totalAvailTime as "Total Available Time", totalAuxOthers as"Total Aux Others", extnCalls as "Extension Calls", avgExtnTime as "Average Extension Time", totalTimedStaff as "Total Time Staffed", totalHoldTime as "Total Hold Time" From AvayaCMSAT_agent&windowWidth=' + windowDims[2];
var loader = dhtmlxAjax.getSync(url);
if (loader.xmlDoc.responseText.length > 0) {
grid9.parse(loader.xmlDoc.responseText, 'xml');
grid9.loadSortingFromCookie('grid9');
grid9.loadSizeFromCookie('grid9');
}
}
catch (err) {}
}
if (w3.isHidden() == false) {
try {
var windowDims = getWindowStatus(3, 'grid');
var url = site + '/user.widget?page=just_data&query=Select groupName as "Group Name", groupNumber as "Group Number", percentInServiceLevel as"Percent Within Service Level", totalAcdCalls as "Total Acd Calls", avgSpeedAnswer as "Average Speed Answer", abandonCalls as "Abandoned Calls", avgAbandonTalkTime as "Average Abandon Talk Time", avgTalkTime as "Average Talk Time", totalAfterCall as "Total After Call", flowIn as "Flow In", flowOut as "Flow Out", totalAuxOther as " Total Aux/Other", avgStaff as" Average Staff" From AvayaCMSAT_skill&windowWidth=' + windowDims[2];
var loader = dhtmlxAjax.getSync(url);
if (loader.xmlDoc.responseText.length > 0) {
grid3.parse(loader.xmlDoc.responseText, 'xml');
grid3.loadSortingFromCookie('grid3');
grid3.loadSizeFromCookie('grid3');
}
}
catch (err) {}
}
if (w4.isHidden() == false) {
try {
var url = site + '/user.widget?page=just_data&query=Select 0 AS"Min", ROUND((((SUM(totalAcdCalls * percentInServiceLevel)/100)*1.0)/SUM(totalAcdCalls))*100,2) AS"Cur",100 AS "Max" From AvayaCMSAT_skill';
var loader = dhtmlxAjax.getSync(url);
var text = loader.xmlDoc.responseText;
var dataCells = text.split("<cell>");
if (dataCells.length >= 3) {
var speedoValue = dataCells[2];
speedoValue = speedoValue.substring(0, speedoValue.indexOf("<"));
$('#speedometer4_div').speedometer({
percentage: speedoValue || 0
});
}
}
catch (err) {}
}
if (w12.isHidden() == false) {
try {
var windowDims = getWindowStatus(12, 'grid');
var url = site + '/user.widget?page=just_data&query=Select vdnName as "Vdn Name", extSkill as "Skill/Extension", num as " Vector Number", callsWaiting as "Calls Waiting", longestWaitingCall as "Longest Waiting Call", acdCalls as "Acd Calls", avgSpeedTalk as"Average Speed Talk", abandonedCalls as "Abandoned Calls", avgAbandonTime as "Average Abandon Time", avgTalkHold as "Average Talk Hold", connCalls as "Conference Calls", flowOut as "Flow Out", callBusyDisc as "Call Busy Disconnect", percentInServLevel as "Percent In Service Level" From AvayaCMSAT_vdn WHERE meas !="none"&windowWidth=' + windowDims[2];
var loader = dhtmlxAjax.getSync(url);
if (loader.xmlDoc.responseText.length > 0) {
grid12.parse(loader.xmlDoc.responseText, 'xml');
grid12.loadSortingFromCookie('grid12');
grid12.loadSizeFromCookie('grid12');
}
}
catch (err) {}
}
if (w13.isHidden() == false) {
try {
var windowDims = getWindowStatus(13, 'grid');
var url = site + '/user.widget?page=just_data&query=Select vdnName as "Vdn Name", extSkill as "Skill/Extension", num as " Vector Number", callsOffered as "Calls Offered", totalCalls as "Total Acd Calls", avgSpeedAnswered as "Avg Speed Answered", totalAbandonedCalls as"Aboanded Calls", dayAvgAbandonTalk as "AVG Abandon Talk Time", dayAvgTalkHold as "AVG Talk Hold", totalConnCalls as "Conn Calls", totalFlowOut as "Flow Out", totalCallBusyDisc as "Call Busy Disc", prctInServLevel as "Percent In Service Level" From AvayaCMSAT_vdn WHERE meas !="none"&windowWidth=' + windowDims[2];
var loader = dhtmlxAjax.getSync(url);
if (loader.xmlDoc.responseText.length > 0) {
grid13.parse(loader.xmlDoc.responseText, 'xml');
grid13.loadSortingFromCookie('grid13');
grid13.loadSizeFromCookie('grid13');
}
}
catch (err) {}
}
如果您發現上面的字符串是有重複的圖案總是具有以下begining:
if (w[number].isHidden() == false){
我感興趣後,最終右括號之前出現的東西。因此,例如在這種情況下:
if (w2.isHidden() == false) {
//PART TO EXTRACT
var counterValue = 0;
try {
var url = site + '/user.widget?page=just_data&query=Select IFNULL(time(MAX(longestWaitingSeconds), "unixepoch") , time(0, "unixepoch"))From vayaCMSAT_skill';
var loader = dhtmlxAjax.getSync(url);
var text = loader.xmlDoc.responseText;
var dataCells = text.split("<cell>");
if (dataCells.length >= 2) {
counterValue = dataCells[1];
counterValue = counterValue.substring(0, counterValue.indexOf("<"));
}
}
catch (err) {}
var elem = document.getElementById("counter2_Value");
elem.innerHTML = counterValue;
//END PART TO EXTRACT
}
正如以上強調我在提取所述開口之間的字符串和關閉{},並把它在一個數組intrested。我希望能夠爲orignal響應正文字符串中顯示的所有if語句執行此操作。
我已經看過用regular expressions和的indexOf()/串(),例如:
var start = html.indexOf("false) {", 0) + 8;
var end = html.indexOf("if (w", start);
alert(html.substring(start, end));
但我沒有設法打開和關閉大括號中的字符串。
是否有可能爲此創建一個'jsfiddle'鏈接,以便可以在其中嘗試修復。 – anubhava
@anubhava嗨,我只是設法解決這個問題,我將很快發佈解決方案我結束了使用字符串匹配和子字符串不是最傳統的方式,但正在爲我的需要工作。非常感謝您的關注。 – user1010101
確定讓我看看。 – anubhava