DOMDOCUMENT PARSER UN TABLEAU PRECIS PARMI PLUSIEURS
Posté : 18 févr. 2011, 00:26
bonjour
pouvez vous me dire s'il est possible (avec DomDocument et php) de recuperer les données presentes uniquement dans les tableaux ayant pour class='tableauOK' sachant que:
- le nb de tableaux ayant pour class='tableauOK' peut varier
- le nb de tableaux ayant pour class="SYSTEM" peut aussi varier
ci dessous le code html à parser et le debut de code
mais je ne vois pas comment faire pour recuperer les données que des tableaux avec class='tableauOK'.
On m'a parle de DOMXPath mais je ne vois pas comment faire
merci pour votre aide
NR
pouvez vous me dire s'il est possible (avec DomDocument et php) de recuperer les données presentes uniquement dans les tableaux ayant pour class='tableauOK' sachant que:
- le nb de tableaux ayant pour class='tableauOK' peut varier
- le nb de tableaux ayant pour class="SYSTEM" peut aussi varier
ci dessous le code html à parser et le debut de code
mais je ne vois pas comment faire pour recuperer les données que des tableaux avec class='tableauOK'.
On m'a parle de DOMXPath mais je ne vois pas comment faire
merci pour votre aide
NR
<table id="tab" class="SYSTEM" cellpadding="0" cellspacing="0">
<thead>
<tr>
<th>N</th><th>Name</th><th>Results</th>
</tr>
</thead>
<tbody>
<tr class="info">
<td class="num"><a href="/page/menu.html?id=98">1</a></td>
<td><a href="/page/menu.html?id=78"><span>TEST1</span></a></td>
<td><a href="/page/menu.html?id=7798">value=1</a></td>
</tr>
<tr class="info">
<td class="num"><a href="/page/menu.html?id=9">5</a></td>
<td><a href="/page/menu.html?id=8"><span>TEST2</span></a></td>
<td><a href="/page/menu.html?id=98">value=7</a></td>
</tr>
<tr class="info">
<td class="num"><a href="/page/menu.html?id=9">789</a></td>
<td><a href="/page/menu.html?id=9"><span>TEST3</span></a></td>
<td><a href="/page/menu.html?id=900">value=77</a></td>
</tr>
</tbody>
</table>
<table class='tableauOK' border='0' cellpadding='0' cellspacing='0' id='TableOK'>
<thead>
<tr><th width='8px'>DATE</th><th width='400px'>VALUE</th><th>STATUS</th></tr>
<tr>
<td style='text-align:center'><strong>06/01/2011</strong></td>
<td><strong><a href='/TEST/page.html?id=71'>x=12</a></td>
<td><strong>ok</strong></td>
</tr>
<tr>
<td style='text-align:center'><strong>07/02/2011</strong></td>
<td><strong><a href='/TEST/page.html?id=22'>x=1230</a></td>
<td><strong>NO</strong></td>
</tr>
<tr>
<td style='text-align:center'><strong>01/02/2010</strong></td>
<td><strong><a href='/TEST/page.html?id=7'>x=11</a></td>
<td><strong>OK</strong></td>
</tr>
</table>
<table class='tableauOK' border='0' cellpadding='0' cellspacing='0' id='TableOK'>
<thead>
<tr><th width='8px'>DATE</th><th width='400px'>VALUE</th><th>STATUS</th></tr>
<tr>
<td style='text-align:center'><strong>16/03/2009</strong></td>
<td><strong><a href='/TEST/page.html?id=761'>x=16</a></td>
<td><strong>ok</strong></td>
</tr>
<tr>
<td style='text-align:center'><strong>12/01/2011</strong></td>
<td><strong><a href='/TEST/page.html?id=220'>x=2</a></td>
<td><strong>NO</strong></td>
</tr>
<tr>
<td style='text-align:center'><strong>01/01/2011</strong></td>
<td><strong><a href='/TEST/page.html?id=8'>x=1100</a></td>
<td><strong>NO</strong></td>
</tr>
</table>
<table class='tableauOK' border='0' cellpadding='0' cellspacing='0' id='TableOK'>
<thead>
<tr><th width='8px'>DATE</th><th width='400px'>VALUE</th><th>STATUS</th></tr>
<tr>
<td style='text-align:center'><strong>16/02/2011</strong></td>
<td><strong><a href='/TEST/page.html?id=761'>x=15</a></td>
<td><strong>ok</strong></td>
</tr>
<tr>
<td style='text-align:center'><strong>12/02/2011</strong></td>
<td><strong><a href='/TEST/page.html?id=220'>x=1000</a></td>
<td><strong>NO</strong></td>
</tr>
<tr>
<td style='text-align:center'><strong>01/02/2011</strong></td>
<td><strong><a href='/TEST/page.html?id=7'>x=1100</a></td>
<td><strong>OK</strong></td>
</tr>
</table>
<?php
/*** a new dom object ***/
$dom = new domDocument;
/*** load the html into the object ***/
//$dom->loadHTML($html);
@$dom->loadHTMLFile($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.'<br />';
echo $cols->item(1)->nodeValue.'<br />';
echo $cols->item(2)->nodeValue;
echo '<hr />';
}
?>