DOMXPath Help

Hi Guys,

Slowly but surely i’m getting my head around loading html into a domDocument to parse out parts i’m looking for.

currently i have:


<?php
// parse the html into a DOMDocument  
$dom = new DOMDocument();
$dom->loadHTML($html);
       
$xpath   = new DOMXPath($dom);
$results = $xpath->query("/html/body/div[3]/center/table/tbody/tr/td/table/tbody/tr/td[2]/div[2]/table/tbody/tr/td");

foreach ($results as $result) {

}
?>

i got the xPath location by using xpather for firefox, the piece of html i’m trying to loop over and get values from is:


<acronym title="Affiliate Info: Pays  % on Level1  Seller Accepts   PAYPAL "><table width="95%" border="0" align="center" cellpadding="0" cellspacing="3"><tr align="left"><td colspan="2" class="acat" >TITLE I NEED PARSED<font color="#567faf"> $39.95</font></td>
</tr><tr align="left"><td width="26">&nbsp;</td>
<td class="subtitle_s"><em><font color="#333333"><span>DESCRIPTION I NEED TO PARSE</></font></em></td>
</tr><tr align="left"><td>&nbsp;</td>
<td><a href=a.page.php?id=9911&u=pydtcm >Promote</a> |  <a href=http://site.com/r/9911/XXXXX/ target=_blank>Visit site</a><acronym> | [ APS: <span onClick="window.open('aps.php','','width=500, height=300');" style="color:#0000FF; 
text-decoration:underline; cursor:pointer;">547.32</span>* ]</acronym></td>

is there any way i can print to screen the domDocument once loaded?

i’m not usually this slow with learning lol

thanks guys

Graham

Use the saveXML method on $dom: i.e. echo $dom->saveXML();

Hi Salathe,

Thanks again for the help :slight_smile:

I thought that would echo me some sort of array but i was wrong lol

i have gotten further:


<?php

$dom = new DOMDocument();
$dom->loadHTML($html);
       
$xpath   = new DOMXPath($dom);
$results = $xpath->query("//*[@class='acat']");

foreach ($results as $result) {

// Title
$title = $result->textContent;
            
$xpath = new DOMXPath($dom);
$res   = $xpath->query("//*[@class='subtitle_s']");
            
$desc = $res->textContent;
print $desc;  

}

the title prints out fine from the html above, but this part:

$xpath = new DOMXPath($dom);
$res = $xpath->query(“//*[@class=‘subtitle_s’]”);

$desc = $res->textContent;
print $desc;

was a desperate attempt lol, can you use a query inside the foreach for another part of the html?

thanks mate

Graham