DOMDOCUMENT PARSER UN TABLEAU PRECIS PARMI PLUSIEURS

Eléphanteau du PHP | 12 Messages

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

<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 />'; 
} 
?>

devlop78
Invité n'ayant pas de compte PHPfrance

18 févr. 2011, 00:50

un truc comme
$tables = $document->getElementsByTagName ('table');

foreach ($tables as $currentTable) {

$classAttr = $currentTable->attributes->getNamedItem('class');

if (strpos(" tableauOk",$classAttr) !== false || strpos("tableauOk ",$classAttr) !== false) {

// C'est un tableau comme il faut (ça rime)

}

}

Eléphanteau du PHP | 12 Messages

27 févr. 2011, 15:39

merci devlop78 pour ton aide :wink:

ViPHP
ViPHP | 5462 Messages

27 févr. 2011, 19:05

le xptah est :
//table[@class="tableauOK"]/tr/td

Eléphanteau du PHP | 12 Messages

27 févr. 2011, 20:31

bonjour stealth35

savez vous pourquoi en utilisant le xptah //table[@class="tableauOK"]/tr/td
le resultat est 0

Code : Tout sélectionner

<?php $file = "montest.html"; $dom = new DOMDocument(); @$dom->loadHTMLFile($file); $xPath = new DOMXPath($dom); $entries = $xPath->query('//table[@class="tableauOK"]/tr/td'); echo $entries->length; foreach ($entries as $element) { echo "<br/>". $element->nodeName. ": "; $nodes = $element->childNodes; foreach ($nodes as $node) { //echo $node->nodeValue. "<br/>"; echo $node->nodeValue. "\n"; } } ?>

ViPHP
ViPHP | 5462 Messages

27 févr. 2011, 21:20

j'avais pas vu les thead donc :
//table[@class="tableauOK"]/thead/tr/td

sinon on peux toujours faire :
//table[@class="tableauOK"]//td


ps : vérifie ton code HTML il n'est pas valide.

Eléphanteau du PHP | 12 Messages

27 févr. 2011, 21:47

MERCI stealth35 pour ta reponse rapide

sais tu où je peux trouver un bon tutoriel pour m'expliquer xptah et query() methode?

quand entre un <td> et </td> j'ai un lien , comment recuperer les attributs href du lien?

Nuno

ViPHP
ViPHP | 5462 Messages

27 févr. 2011, 22:08

MERCI stealth35 pour ta reponse rapide

sais tu où je peux trouver un bon tutoriel pour m'expliquer xptah et query() methode?

quand entre un <td> et </td> j'ai un lien , comment recuperer les attributs href du lien?

Nuno
si tu veux que récupèrer les liens fait :
//table[@class="tableauOK"]/thead/tr/td/a/@href
ou
//table[@class="tableauOK"]//a/@href

sinon, DOMElement a la méthode getAttribute

pour xpath :
http://www.w3.org/TR/xpath/
https://developer.mozilla.org/fr/XPATH

Eléphanteau du PHP | 12 Messages

27 févr. 2011, 23:15

UN GRAND MERCI stealth35
tes reponses m'ont bien aidé

Nuno