1

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.

2
  • Welcome to SO. Please take some time and make your question a little bit readable so we could help you better. Commented Jan 6, 2012 at 8:00
  • You are suppressing errors with the @ operator. If an error occurs you will not notice it. E.g. if the document can not be loaded (server down) there would be no node to extract anything. Additionally if you properly indent your code, it would be better readable and much easier to deal with. Commented Jan 6, 2012 at 8:07

4 Answers 4

1

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);
Sign up to request clarification or add additional context in comments.

Comments

1
  // 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;"

Comments

0

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

2 Comments

I dont want to save output into a file , I want to save it into a string or a array to manipulate.
Wish you had clicked on the links I provided, because those 2 functions are NOT for saving to any file. Both of these are just returning content into a string.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.