java如何取到如下html中的div内的字符串“需要取的内容” htmldivjava 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 把html作为xml文档来看待,用dom4j通过XPath可以轻易找到指定内容 dom4j通过XPath我会使用,但取这个位置的值我不会写啊,小弟愚钝,还请各位大侠贴上具体代码吧,谢谢 先通过javascript获取其中的值,再通过链接让值作为参数发给服务端接收 $.each($("#apDiv6 > table > tbody >tr "),function(i){ if($(this).attr("align")=="left"){ alert($(this).text()) } }) 使用dom4j 、xpath 。依赖jar文件 dom4j-1.6.1.jar jaxen-1.1.1.jarpackage com.zf.test;import java.io.StringReader;import org.dom4j.Document;import org.dom4j.Node;import org.dom4j.io.SAXReader;public class XPATH { public static void main(String[] args) throws Exception { String str = "<div id=\"apDiv6\">"+ "<table width=\"100%\" height=\"182\" cellspacing=\"3\" border=\"0\">"+ "<tbody><tr>"+ "<td valign=\"middle\" height=\"147\" align=\"center\"><a href=\"#\"><img width=\"211\" height=\"140\" src=\"../images/group/pic04.jpg\" /></a></td>"+ "</tr>"+ "<tr>"+ "<td align=\"left\"><a style=\" color:#213964\"><strong>需要取的内容</strong></a></td>"+ "</tr>"+ "</tbody></table>"+ "</div>"; SAXReader reader = new SAXReader(); Document doc = reader.read(new StringReader(str)); Node node = doc.selectSingleNode("//div/table/tbody/tr/td/a/strong"); System.out.println(node.getText()); }}方式二: 使用jsoup通过jquery格式搜索html中dom元素 依赖jar文件 jsoup-1.7.1 package com.zf.test;import org.jsoup.Jsoup;import org.jsoup.nodes.Document;import org.jsoup.nodes.Element;import org.jsoup.select.Elements;public class JSOUP { public static void main(String[] args) { String str = "<div id=\"apDiv6\">"+ "<table width=\"100%\" height=\"182\" cellspacing=\"3\" border=\"0\">"+ "<tbody><tr>"+ "<td valign=\"middle\" height=\"147\" align=\"center\"><a href=\"#\"><img width=\"211\" height=\"140\" src=\"../images/group/pic04.jpg\" /></a></td>"+ "</tr>"+ "<tr>"+ "<td align=\"left\"><a style=\" color:#213964\"><strong>需要取的内容</strong></a></td>"+ "</tr>"+ "</tbody></table>"+ "</div>"; Document doc = Jsoup.parse(str); Elements es = doc.select("a strong"); for (Element element : es) { System.out.println(element.html()); } } } 使用dom4j把他看成xml来读取不就ok了String xml=你上面的html脚本字符串SAXReader reader=new SAXReader();Document doc=reader.read(new StringReader(xml));String text=doc.selectSingleNode("/div[@id='apDiv6']//strong[1]").getText();System.out.println(text); <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title></title><meta name="powered-by" content="FineUI v3.2.6 - 基于 ExtJS 的专业 ASP.NET 2.0 控件库 - http://fineui.com/" /><link href="/res.axd?css=notheme.css&v=3" rel="stylesheet" text="text/css"/><link href="/res.axd?css=blue.css&v=3" rel="stylesheet" text="text/css"/><link href="/res.axd?css=ux.ux.css&v=635558012233427301" rel="stylesheet" text="text/css"/><style type="text/css">.fineui_x1_panelbase_icon{background: url(/icon/magnifier.png) no-repeat;}</style></head> struts2和iReport问题 表单中不能使用javascript代码么? 怎么样在tomcat启动时创建java控制台进程或线程? 当sqlserver数据库服务器上有两个实例时,想用实例名连接,url怎么写啊? 各位高手快救救我吧!!! 关于在JBuilder9下编译网页的问题 请问linux下的好的jsp开发平台? 如何配置 JSP 的运行环境! 请问在jsp中调用外部的类,与jsp调用javabean有何区别?? Activemq+Spring做远程消息发送 jQuery+ajax验证用户名是否存在,火狐能成功,IE不行,为什么? hibernate
if($(this).attr("align")=="left"){
alert($(this).text())
}
})
import org.dom4j.Node;
import org.dom4j.io.SAXReader;public class XPATH {
public static void main(String[] args) throws Exception {
String str = "<div id=\"apDiv6\">"+
"<table width=\"100%\" height=\"182\" cellspacing=\"3\" border=\"0\">"+
"<tbody><tr>"+
"<td valign=\"middle\" height=\"147\" align=\"center\"><a href=\"#\"><img width=\"211\" height=\"140\" src=\"../images/group/pic04.jpg\" /></a></td>"+
"</tr>"+
"<tr>"+
"<td align=\"left\"><a style=\" color:#213964\"><strong>需要取的内容</strong></a></td>"+
"</tr>"+
"</tbody></table>"+
"</div>";
SAXReader reader = new SAXReader();
Document doc = reader.read(new StringReader(str));
Node node = doc.selectSingleNode("//div/table/tbody/tr/td/a/strong");
System.out.println(node.getText()); }}方式二: 使用jsoup通过jquery格式搜索html中dom元素 依赖jar文件 jsoup-1.7.1 package com.zf.test;import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;public class JSOUP { public static void main(String[] args) {
String str = "<div id=\"apDiv6\">"+
"<table width=\"100%\" height=\"182\" cellspacing=\"3\" border=\"0\">"+
"<tbody><tr>"+
"<td valign=\"middle\" height=\"147\" align=\"center\"><a href=\"#\"><img width=\"211\" height=\"140\" src=\"../images/group/pic04.jpg\" /></a></td>"+
"</tr>"+
"<tr>"+
"<td align=\"left\"><a style=\" color:#213964\"><strong>需要取的内容</strong></a></td>"+
"</tr>"+
"</tbody></table>"+
"</div>";
Document doc = Jsoup.parse(str);
Elements es = doc.select("a strong");
for (Element element : es) {
System.out.println(element.html());
}
}
}
SAXReader reader=new SAXReader();
Document doc=reader.read(new StringReader(xml));
String text=doc.selectSingleNode("/div[@id='apDiv6']//strong[1]").getText();
System.out.println(text);
<head><title></title>
<meta name="powered-by" content="FineUI v3.2.6 - 基于 ExtJS 的专业 ASP.NET 2.0 控件库 - http://fineui.com/" />
<link href="/res.axd?css=notheme.css&v=3" rel="stylesheet" text="text/css"/>
<link href="/res.axd?css=blue.css&v=3" rel="stylesheet" text="text/css"/>
<link href="/res.axd?css=ux.ux.css&v=635558012233427301" rel="stylesheet" text="text/css"/>
<style type="text/css">.fineui_x1_panelbase_icon{background: url(/icon/magnifier.png) no-repeat;}</style>
</head>