HTML DOM文档解析 [英] HTML DOM Document parsing
本文介绍了HTML DOM文档解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是新的DOM文档..
i有这个html:
i am new to DOM Document.. i have this html:
<tr class="calendar_row" data-eventid="39657">
<td class="alt1 eventDate smallfont" align="center">Sun<div class="eventday_multiple">Dec 9</div></td>
<td class="alt1 smallfont" align="center">3:34am</td>
<td class="alt1 smallfont" align="center">USD</td>
</tr>
<tr class="calendar_row" data-eventid="39658">
<td class="alt1 eventDate smallfont" align="center">Sun<div class="eventday_multiple">Dec 10</div></td>
<td class="alt1 smallfont" align="center">5:14am</td>
<td class="alt1 smallfont" align="center">EUR</td>
</tr>
我试图使用以下代码获取tr中的内容:
i am trying to get first the contents inside the tr's using this code:
$ret = array();
libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadHTML($html);
//$doc->saveHTMLFile('textbox.php');
$text = $doc->getElementsByTagName('tr');
foreach ($text as $tag){
$ret[] = $doc->saveHtml($tag);
echo $doc->saveHtml($tag);
}
我不知道为什么回显的值是整个文档,而不是值里面的tr的..
i dont know why the value being echoed was the whole document and not the values inside the tr's..
第二,我也想得到这些td标签之间的值,如5:14 AM,EUR等。但是我没有任何想法如何做到这一点。
second, i would like also to get the values in between those td tags like 5:14 AM,EUR,etc. but i dont have any idea how to do that.
赦免noob问题..
Pardon for noob question..
最好的问候
推荐答案
$doc = new DOMDocument();
$doc ->loadHTML("$html");
$tables = $doc->getElementsByTagName('table');
$table = $tables->item(0);//takes the first table in dom
foreach ($table->childNodes as $td) {
if ($td->nodeName == 'td') {
echo $td->nodeValue, "\n";
}
}
这篇关于HTML DOM文档解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文