sun kum
sun kum

Reputation: 27

convert a nodevalue into a string

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

Answers (4)

sun kum
sun kum

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

Dan Blows
Dan Blows

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

Humble
Humble

Reputation: 61

did you try @$cols->item(0)->textContent

Upvotes: 0

anubhava
anubhava

Reputation: 785186

Use DOM::saveXML or DOM::saveHTML to convert node value to string.

Upvotes: 0

Related Questions