java中写html脚本_解析Java脚本中HTML标记的最佳方法

$HTML=

&!doctype html public“-//w3c//dtd xhtml 1.0严格//en”

“http://www.w3.org/tr/xhtml1/dtd/xhtml1 strict.dtd”>

&头;

phpro.org

和/头& GT;

和身体;

周六预测

&!--2008年5月23日星期五8时28分发布-->

TSBOR>

T>

悉尼19°好。多半阳光明媚。

& LT/TR & GT;

T>

墨尔本16°雾然后很好。

& LT/TR & GT;

T>

布里斯班24°大部分都很好

& LT/TR & GT;

T>

珀斯21°淋浴次数少,稍后增加。

& LT/TR & GT;

T>

阿德莱德20°好。多半阳光明媚。

& LT/TR & GT;

T>

霍巴特13°主要是好的。

& LT/TR & GT;

T>

堪培拉15°很好,大部分是晴天。

& LT/TR & GT;

T>

达尔文32°阳光明媚。

& LT/TR & GT;

&T/BOT>

&表;

和/身体;

& lt//html & gt;

;

/***新的DOM对象***/

$dom=新建domdocument;

/***将HTML加载到对象中***/

$dom->加载HTML($html);

/***放弃空白***/

$dom->preserveWhitespace=false;

/***表的标签名***/

$tables=$dom->getElementsByTagname('table');

/***从表中获取所有行***/

$rows=$tables->项(0)->getElementsByTagname('tr');

/***循环表行***/

foreach($rows作为$row)

{

/***按标记名获取每列***/

$cols=$row->getElementsByTagname(“td”);

/***返回值***/

echo$cols->项(0)->节点值。'
';

echo$cols->项(1)->节点值。'
';

echo$cols->项(2)->节点值;

echo'


';

}

?gt;

< /代码>

出http://www.php.net/manual/en/book.dom.php

以下是由提供的示例PHP Pro

$html = '

/p>

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

PHPRO.ORG

Forecast for Saturday

Sydney19°Fine. Mostly sunny.
Melbourne16°Fog then fine.
Brisbane24°Mostly fine
Perth21°Few showers, increasing later.
Adelaide20°Fine. Mostly sunny.
Hobart13°Mainly fine.
Canberra15°Fine, mostly sunny.
Darwin32°Fine and sunny.

';

/*** a new dom object ***/

$dom = new domDocument;

/*** load the html into the object ***/

$dom->loadHTML($html);

/*** discard white space ***/

$dom->preserveWhiteSpace = false;

/*** the table by its tag name ***/

$tables = $dom->getElementsByTagName('table');

/*** get all rows from the table ***/

$rows = $tables->item(0)->getElementsByTagName('tr');

/*** loop over the table rows ***/

foreach ($rows as $row)

{

/*** get each column by tag name ***/

$cols = $row->getElementsByTagName('td');

/*** echo the values ***/

echo $cols->item(0)->nodeValue.'
';

echo $cols->item(1)->nodeValue.'
';

echo $cols->item(2)->nodeValue;

echo '


';

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值