Friday, 6 September 2013

DomDocument Query

DomDocument Query

Hi I am very new to this World of DomDocument,Im still learning and
looking for xpath query use in DomDocument.The html sometimes changes so a
preg_match is not a good idea. .I need to get the values from a html
file.This is the part of html i want to get. I would be happy if you could
help me..
<?php
$doc = new DOMDocument();
@$doc->loadHTML('<table cellspacing="0" cellpadding="0" align="center"
class="results">
<tr class="header" bgcolor="#0000FF">
<td>
</td>
<td>Name/AKAs</td>
<td>Age</td>
<td>Location</td>
<td>Possible Relatives</td>
</tr>
<tr>
<td>1.</td>
<td>
<a class="LN" href=""><b>Iron, Man E</b></a>
</td>
<td align="center">54</td>
<td>
<a href="">Canada, AK</a><br />
<a href="">California, AK</a><br />
</td>
<td>
</td>
<td>
<a href="">View Details</a>
</td>
</tr>
<tr><td>2.</td>
<td>
<a class="LN" href=""><b>Bat, Man E</b></a></td>
<td align="center">26</td>
<td>
<a href="">Gotham, IA</a>
<br /></td>
<td>
<a href=""> View Details</a></td></tr>
</table>');
$xpath = new DOMXPath($doc);
$xquery = '//a[@class="LN"]';
$links = $xpath->query($xquery);
foreach ($links as $el) {
echo strip_tags($doc->saveHTML($el)).'<br/>';
}
?>
How do i get the following value? I can only get Iron, Man E, and Bat, Man E
Iron, Man E | 54 | Canada, AK;California, AK
Bat, Man E | 26 | Gotham, IA

No comments:

Post a Comment