Page 1 of 1

I want to retrieve the value of href attribute

Posted: Thu Feb 25, 2010 9:53 am
by priyanka1985
I want to retrieve the value of href tag and then navigate to the page and retrieve some other data.
Please find attached a snippet,

<table>
<tr>
<td></td>
<td></td>
<td></td>
<td><a href="http://www.xyz.com" target="abc"></a></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td><a href="http://www.pqr.com" target="mnp"></a></td>
</tr>
</table>

I am currently using loadHTML() to parse the results for <tr> and <td> tags but i am unable to parse the href attribute of <a> tag. How can we get the value of href attribute i.e. http://www.xyz.com in this case?

Re: I want to retrieve the value of href attribute

Posted: Thu Feb 25, 2010 10:44 am
by AbraCadaver
What did you try?

getElementsByTagName()
getAttribute()

Re: I want to retrieve the value of href attribute

Posted: Thu Feb 25, 2010 4:14 pm
by priyanka1985
Hey, I am currently using the following code for parsing,

$tables = $dom->getElementsByTagName("table");
$rows = $tables->item(1)->getElementsByTagName("tr");
foreach($rows as $row){
$cols = $row->getElementsByTagName("td");
echo @$cols->item(0)->nodeValue;
echo @$cols->item(1)->nodeValue;
echo @$cols->item(2)->nodeValue;
echo @$cols->item(3)->nodeValue;
echo "<br />";
}
}

But i am not sure how to incorporate the code for <a> tag. Can you please help?

Re: I want to retrieve the value of href attribute

Posted: Thu Feb 25, 2010 4:18 pm
by AbraCadaver
Not trying to be rude, but if you use this:

$tables = $dom->getElementsByTagName("table"); to get <table> tags, what do you think you would change to get <a> tags?