Reputation: 27
Working on dom html . I want to convert node value to string:
$html = @$dom->loadHTMLFile('url');
$dom->preserveWhiteSpace = false;
$tables = $dom->getElementsByTagName('body');
$rows = $tables->item(0)->getElementsByTagName('tr');
// loop over the table rows
foreach ($rows as $text =>$row)
{
$t=1;
// get each column by tag name
$cols = $row->getElementsByTagName('td');
//getting values
$rr = @$cols->item(0)->nodeValue;
print $rr; ( it prints values of all 'td' tag fine)
}
print $rr; ( it prints nothing) I want it to print here
?>
I want nodevalues to be converted into string for further manipulation.
Upvotes: 1
Views: 2868
Reputation: 27
// new dom object
$dom = new DOMDocument();
//load the html
$html = @$dom->loadHTMLFile('http://webapp-da1-01.corp.adobe.com:8300/cfusion/bootstrap/');
//discard white space
$dom->preserveWhiteSpace = false;
//the table by its tag name
$tables = $dom->getElementsByTagName('head');
//get all rows from the table
$la=array();
$rows = $tables->item(0)->getElementsByTagName('tr');
// loop over the table rows
$array = array();
foreach ($rows as $text =>$row)
{
$t=1;
$tt=$text;
// get each column by tag name
$cols = $row->getElementsByTagName('td');
// echo the values
#echo @$cols->item(0)->nodeValue.'';
// echo @$cols->item(1)->nodeValue.'';
$array[$row] = @$cols->item($t)->nodeValue;
}
print_r ($array);
It prints Array ( ) nothing more. i also used "$cols->item(0)->nodeValue;"
Upvotes: 1
Reputation: 21174
Every time you loop through the foreach
you overwrite the value of the $rr
variable. The second print $rr
will print the value of the last td
- if it's empty, then it will print nothing.
If what you are trying to do is print all the values, instead write them to an array:
$rr = array();
foreach($rows as $text =>$row) {
$rr[] = $cols->item(0)->nodeValue;
}
print_r($rr);
Upvotes: 1
Reputation: 785186
Use DOM::saveXML or DOM::saveHTML to convert node value to string.
Upvotes: 0