使用Cobra解析html, 及其提取table内容的例子。
?? ?以下方法读取一个html格式的string,然后把其中的table内容读出,存入一个arralyst, 该list装入的是string[], 即每一个string[]元素代码table的一行。
?
private list parsetable(string htmlstr) throws saxexception, ioexception {reader reader = new stringreader(htmlstr);inputsourceimpl inputsource = new inputsourceimpl(reader,"aaa");useragentcontext uacontext = new simpleuseragentcontext();documentbuilderimpl builder = new documentbuilderimpl(uacontext);document d = builder.parse(inputsource);htmldocumentimpl document = (htmldocumentimpl) d;nodelist nl=document.getelementsbytagname("table");arraylist<string[]> records=new arraylist<string[]>();for (int i = 0; i < nl.getlength(); i++) {htmltableelementimpl table=(htmltableelementimpl)nl.item(i);htmlcollection rows=table.getrows();for (int j = 0; j < rows.getlength(); j++) {htmltablerowelementimpl row =(htmltablerowelementimpl)rows.item(j);htmlcollection cells=row.getcells();arraylist<string> cellsstr=new arraylist<string>();for (int k = 0; k < cells.getlength(); k++) {htmltablecellelementimpl cell =(htmltablecellelementimpl)cells.item(k);cellsstr.add(cell.gettextcontent().tostring());}records.add(cellsstr.toarray(new string[0]));}} return records;}??