
一个简单的流程图
1HTML
<div id="myDiagram" style="width:400px; height:150px; background-color: #DAE4E4;"></div>
2$是一个全局变量,全局变量相当于一个画笔,我们先定义一个画笔工具。之后我们就可以通过 $ 符号来调用它自身的一些方法、属性、枚举对象等
var $ = goGraphObjectmake;//
3有了画笔,还需要画布,于是我们需要定义一个画布用来装载一些具体内容
var myDiagram =$(goDiagram, //定义的画布
"myDiagram",//要渲染的DIV 的Id 名称,1中html中的id
{
initialAutoScale: goDiagramUniform,
contentAlignment: goSpotCenter,//对齐方式
layout://画布里的元素布局方式
$(goForceDirectedLayout,{
defaultSpringLength: 30,
defaultElectricalCharge: 100
})
});
这个画笔可以绘制节点、链接、区域、图案、形状、文本等
4画笔绘制了一个文本对象 goTextBlock ,并为它填充了属性 text、stroke、font、margin
$(goTextBlock,{
1
这种方法有两大步第一步是拼接需要的json数据,第二步是用servlet的内置对象response返回到前台。
2
String 类型的数据可以不用借助任何工具直接返回,只要把它拼接对了就可以。如我需要返回一个{“success”:true,“msg”:“修改失败!”}的json,就可以如下图这样写。(注意,java里的引号要用的转义字符“\”)
3
如果需要返回的是一个list或者别的类的化,需要用到JSONArray的辅助工具类,然后使用responsegetWriter()print(),返回到打到前台。具体代码如下图。
END
方法2:用Spring框架
如果你使用了Spring框架那就更简单了,你只需要在你的方法返回的时候加一个@ResponseBody的注解就可以了。就这么简单。
要扣除里面的表格数据,该怎么弄呢?思路是先把整个表格抠出来,然后按照tr进行分割,存到数组,然后一个个遍历进行正则匹配,最后输出结果转化为json格式,供前台调用!<phpclass getHtmlChar {
var $url; var $pattern_find; var $pattern_replace; var $timeout; /
@param url 需要抓取的url链接
@param pattern_find 需要匹配的字段正则表达式
@param pattern_replace 需要替换的字符正则表达式
@param timeout 设置超时阻断程序执行,默认10秒,这个字段一般留空
/
function __construct($url, $pattern_find, $pattern_replace, $timeout) {
$this->url
= $url; $this->pattern_find = $pattern_find;
$this->pattern_replace = $pattern_replace; $this->timeout =
10;
} // 获取网页源代码
function getHtml() {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,
CURLOPT_USERAGENT, 'Mozilla/50 (Windows NT 61; WOW64)
AppleWebKit/53736 (KHTML, like Gecko) Chrome/3401847131
Safari/53736'); //模拟浏览器,防止被拦截
curl_setopt($ch,
CURLOPT_CONNECTTIMEOUT, $this->timeout); $html =
curl_exec($ch); // 匹配需要的大的字段,例如一段table,取出来之后再一行一行做匹配
preg_match($this->pattern_find, $html, $match); //判断是否有传入替换的正则表达式,如果有就进行匹配替换,并返回替换后的字符串,如果没有,则直接返回匹配出来的大段,例如整个table表格
// echo "<pre>";
// print_r($match);
// echo "</pre>";
// exit;
if (!empty($this->pattern_replace)) { return preg_replace($this->pattern_replace, '', $match[0]);
} else { return $match[0];
}
} // 传入正则和需要处理的字符串,返回的是匹配到的数组
function prgMatche($pre_all, $strs) {
if (!is_string($strs)) { echo '传入的不是字符串!请检查!<br>'; echo "<pre>";
print_r($strs); echo "</pre>"; exit;
}
preg_match_all($pre_all, $strs, $matches); return $matches;
} // 获取表格表头
function getTableJson() {
// 获取一行一行tr
$trs = $this->prgMatche('/<tr([\s\S]+)<\/tr>/i', $this->getHtml()); // 获取表头
$ths = $this->prgMatche('/<th>([\s\S]+)<\/th>/i', $trs[1][0]); // 获取行数,循环匹配的时候需要用到
$num = sizeof($trs[1]); // echo $num;
// 一行一行匹配表格数据
for
($i = 1; $i < $num; $i++) { $tr =
$this->prgMatche('/td>([\s\S]+)<\/td>/i', $trs[1][$i]);
$tds[] = $tr[1];
} // 把表头和表数据存入json
array_unshift($tds, $ths[1]); // 返回一个数组,0为json格式数据,1为数组格式数据
return [json_encode($tds), $tds];
}
}//
以下是调用这个类的例子$url = 'http://wwwpm25in/chaozhou';$pattern_find =
'/<table([\s\S]+)<\/table>/i';$pattern_replace =
'/\s{2,}|\n|<br>| class="O3_8h_dn"/i';$gtd = new getHtmlChar($url,
$pattern_find, $pattern_replace, '');$time_start = microtime(true);echo
"<pre>";
print_r($gtd->getTableJson());echo "</pre>";$time_end = microtime(true);
printf("这段程序运行的时间为 %s 秒!", $time_end - $time_start);>
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)