由上一篇文章也可知:<H1 class="">与<td class="">恰好相反。
但是我们要提取他们里面的同一类型内容(时间),具体信息如下:
<font color="#808080">1730</font>发布时间:<font color="#808080">2005-11-1 15:08:16</font>;
<h2 class="ContentAuthor">作者:lm 日期:2007-04-05</h2>。
尝试了很多办法,都没有成功。适用于第一个信息的方法不适合第二个,反之亦然。最后通过打印出node节点的详细信息后,我才顺利完成任务。具体解决方法如下:
- Node time_node=time_nodes.elementAt(i);
- System.out.println(time_node.toHtml());
- /*
- * 效果如下:
- * F:/computer.hdu.edu.cn/ly/contentid=753.asp
- <font color="#808080">
- <font color="#808080">
- F:/computer.hdu.edu.cn/ly/articleid=9.asp
- <h2 class="ContentAuthor">作者:lmz 日期:2007-04-05</h2>
- */
- String check=time_node.toHtml();
- if(check.indexOf('#')!=-1)
- {
- indextime=time_node.getNextSibling().toHtml();
- }
- else
- indextime=time_node.getChildren().toHtml();
下面是我尝试过的一些错误方法,略过略过……
- try{
- indextime=time_node.getNextSibling().toHtml();
- }
- catch(Exception e){
- System.out.println("hello");
- }
- if(indextime==null||indextime=="")
- {
- this.getParser().reset();
- NodeList time_nodes1=this.getParser().parse(time_filter);
- Node time_node1=time_nodes1.elementAt(i);
- indextime=time_node1.getChildren().toHtml();
- }
- */
- /*
- Node node=time_nodes.elementAt(i);
- NodeList node1=node.getChildren();
- indextime=node1.elementAt(0).toHtml();
- */
- /*
- * 用Node类,其对象不能用getChildren。
- * 而用tag类,则可以。
- */
乱七八糟,一塌糊涂
集成http://blog.csdn.net/caoxu1987728/archive/2008/10/04/3016042.aspx里面的代码就是一篇比较完整的代码!