2017-04-19 66 views
0

我想從xml文件中使用xpath節點js來檢索信息作爲變量以便稍後使用信息以幾張表的形式我嘗試這一小段代碼,但是我出現以下錯誤。我把所有的代碼和附加文件:node js xpath解析html表

滴度

滴度

<p> 
    <FONT COLOR="blue" SIZE='+2'> 
     <B><U> 
    blabkbkba </U></B> 
    </FONT> 
</p> 


<p> 
    <FONT COLOR="red" SIZE='+2'> 
     <B> font </B> 
    </FONT> 
</p> 

<TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0"> 
    <TR bgcolor="#DCDCDC"> 
     <TD><B> info1 </B></TD><TD><B> info2 </B></TD><TD><B> info3 </B></TD> 
     <TD><B> info4 </B></TD><TD><B> info4 </B></TD><TD><B> info5 </B></TD> 
     <TD><B> info6 </B></TD><TD><B> info7 </B></TD><TD><B> info7 </B></TD> 
     <TD><B> info8 </B></TD><TD><B> info9 </B></TD><TD><B> info10</B></TD> 
     <TD><B> info11 </B></TD><TD><B> info12</B></TD> 
    </TR> 

    <TR bgcolor="#B0C4DE"> 
     <TD><B> info11 </B></TD><TD><B> info21 </B></TD><TD><B> info31 </B></TD> 
     <TD><B> info41 </B></TD><TD><B> info41 </B></TD><TD><B> info51 </B></TD> 
     <TD><B> info61 </B></TD><TD><B> info71 </B></TD><TD><B> info71 </B></TD> 
     <TD><B> info81 </B></TD><TD><B> info91 </B></TD><TD><B> info101</B></TD> 
     <TD><B> info111 </B></TD><TD><B> info121</B></TD> 
    </TR> 

</TABLE> 
<TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0"> 
    <TR bgcolor="#DCDCDC"> 
     <TD><B> info1 </B></TD><TD><B> info2 </B></TD><TD><B> info3 </B></TD> 
     <TD><B> info4 </B></TD><TD><B> info4 </B></TD><TD><B> info5 </B></TD> 
     <TD><B> info6 </B></TD><TD><B> info7 </B></TD><TD><B> info7 </B></TD> 
     <TD><B> info8 </B></TD><TD><B> info9 </B></TD><TD><B> info10</B></TD> 
     <TD><B> info11 </B></TD><TD><B> info12</B></TD> 
    </TR> 

    <TR bgcolor="#B0C4DE"> 
     <TD><B> info11 </B></TD><TD><B> info21 </B></TD><TD><B> info31 </B></TD> 
     <TD><B> info41 </B></TD><TD><B> info41 </B></TD><TD><B> info51 </B></TD> 
     <TD><B> info61 </B></TD><TD><B> info71 </B></TD><TD><B> info71 </B></TD> 
     <TD><B> info81 </B></TD><TD><B> info91 </B></TD><TD><B> info101</B></TD> 
     <TD><B> info111 </B></TD><TD><B> info121</B></TD> 
    </TR> 

</TABLE> 
<TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0"> 
    <TR bgcolor="#DCDCDC"> 
     <TD><B> info1 </B></TD><TD><B> info2 </B></TD><TD><B> info3 </B></TD> 
     <TD><B> info4 </B></TD><TD><B> info4 </B></TD><TD><B> info5 </B></TD> 
     <TD><B> info6 </B></TD><TD><B> info7 </B></TD><TD><B> info7 </B></TD> 
     <TD><B> info8 </B></TD><TD><B> info9 </B></TD><TD><B> info10</B></TD> 
     <TD><B> info11 </B></TD><TD><B> info12</B></TD> 
    </TR> 

    <TR bgcolor="#B0C4DE"> 
     <TD><B> info11 </B></TD><TD><B> info21 </B></TD><TD><B> info31 </B></TD> 
     <TD><B> info41 </B></TD><TD><B> info41 </B></TD><TD><B> info51 </B></TD> 
     <TD><B> info61 </B></TD><TD><B> info71 </B></TD><TD><B> info71 </B></TD> 
     <TD><B> info81 </B></TD><TD><B> info91 </B></TD><TD><B> info101</B></TD> 
     <TD><B> info111 </B></TD><TD><B> info121</B></TD> 
    </TR> 

</TABLE> 

<p> 
    <a href="info.jsp">info</a> 
</p> 

這裏是你可以幫助我理解我的錯誤,我使用的第一rtmeps來嘗試恢復TD內容的代碼:我有這樣的反應

var fs  = require('fs'), 
    xpath  = require('xpath'), 
    dom  = require('xmldom').DOMParser, 
    promise = require('promise'); 

function loadDistantFile (url) { 
    return new Promise(function (resolve, reject) { 
    fs.readFile(url, 'utf8', function (err, data) { 
     if (err) { 
     reject(err); 
     } else { 
     resolve(data); 
     } 
    }); 
    }); 
} 

loadDistantFile('./CrewSchedulejspShort.htm').then(function (content) { 
    var doc = new dom().parseFromString(content); 
    var nodes = xpath.select("//TABLE", doc); 

    for (i = 0; i < nodes.length; i++) { 
    var tr  = xpath.select(nodes[i], "//TR").data; 
    var td  = xpath.select(tr[0], "//TD"[0]).data; 
    console.log('td = '+td); 
} 
console.log(nodes[1].toString()); 
    //console.log("Node: " + nodes[0].toString()) 
}).catch(function (err) { 
    console.error('Erreur !'); 
    console.dir(err); 
}); 

ERREUR!

Error: XPath parse error 
    at XPathParser.parse (/Users/user/Desktop/lire html/node_modules/xpath/xpath.js:1185:11) 
    at new XPathExpression (/Users/user/Desktop/lire html/node_modules/xpath/xpath.js:4278:17) 
    at Object.exports.selectWithResolver (/Users/user/Desktop/lire html/node_modules/xpath/xpath.js:4699:19) 
    at Object.exports.select (/Users/user/Desktop/lire html/node_modules/xpath/xpath.js:4682:17) 
    at /Users/user/Desktop/lire html/app.js:31:24 
user:lire html user$ 

回答

0

錯誤發生時您for循環,你嘗試使用您xpath表達式提取裏面的數據。

你目前是什麼,

for (i = 0; i < nodes.length; i++) { 
    var tr  = xpath.select(nodes[i], "//TR").data; 
    var td  = xpath.select(tr[0], "//TD"[0]).data; 
    console.log('td = '+td); 
} 

你應該改變它,

for (i = 0; i < nodes.length; i++) { 
    var tr  = xpath.select("//TR",nodes[i]); 
    var td  = xpath.select("//TD",nodes[i]); 
    console.log('td = '+td); 
} 

這裏是供您參考

var fs  = require('fs'), 
    xpath  = require('xpath'), 
    dom  = require('xmldom').DOMParser, 
    promise = require('promise'); 

function loadDistantFile (url) { 
    return new Promise(function (resolve, reject) { 
    fs.readFile(url, 'utf8', function (err, data) { 
     if (err) { 
     reject(err); 
     } else { 
     resolve(data); 
     } 
    }); 
    }); 
} 

loadDistantFile('./CrewSchedulejspShort.htm').then(function (content) { 
    var doc = new dom().parseFromString(content); 
    var nodes = xpath.select("//TABLE", doc); 

    for (i = 0; i < nodes.length; i++) { 
     //console.log(nodes[i]); 

    /*var tr  = xpath.select(nodes[i], "//TR").data; 
    var td  = xpath.select(tr[0], "//TD"[0]).data;*/ 

    var tr  = xpath.select("//TR",nodes[i]); 
    var td  = xpath.select("//TD",nodes[i]); 
    console.log('td = '+td); 
} 
//console.log(nodes[1].toString()); 
    //console.log("Node: " + nodes[0].toString()) 
}).catch(function (err) { 
    console.error('Erreur !'); 
    console.dir(err); 
}); 
工作代碼

希望這有助於!

+0

感謝您的幫助,但我如何恢復不同的數組,然後不同的tr和td來正確恢復所有信息? – user3210174

+0

您必須像獲取「// TABLE」('var nodes')一樣獲取新數組,然後您將不得不運行另一個「for循環」 –