tags:

views:

162

answers:

3

Hi i have problem with this code, i found it on the internet and when i tested it it gave me the following errorcode:

Parse error: parse error, unexpected $end on line 52

Here is the script:

<?php
function walkDom($node, $level = 0)
{
$indent =";


for ($i = 0; $i nodeType != XML_TEXT_NODE)
{
         echo $indent.''.$node->nodeName.'';


          if( $node->nodeType == XML_ELEMENT_NODE )
          {
          $attributes = $node->attributes; 


              foreach($attributes as $attribute)
              {
               echo ', '.$attribute->name.'='.$attribute->value;

              }
          }


if( strlen(trim($node->childNodes->item(0)->nodeValue)) > 0 && count($cNodes) == 1 ) {
echo ".$indent.'(contains='.$node->childNodes->item(0)->nodeValue.')'; 
}


echo ";
}



$cNodes = $node->childNodes;

if (count($cNodes) > 0)
{
      $level++ ; 
      foreach($cNodes as $cNode) {
      walkDom($cNode, $level); 
      $level = $level – 1; 
      }
}


}

$doc = new DOMDocument();
$doc->loadHTMLFile('http://www.google.se');
walkDom($doc);
?>
+3  A: 

Looks like you have some unclosed string literals.

here:

$indent =";

and here:

echo ";

Those lines should probably be:

$indent ="";

and

echo "";

Also, this line:

for ($i = 0; $i nodeType != XML_TEXT_NODE)

is completely broken. I'm not sure what they were going for here. They probably meant to do this, since $i is never actually used for anything:

while( $node->nodeType != XML_TEXT_NODE )

Overall I'd be a bit suspicious if this code is going to work at all..

Eric Petroelje
I did fix that, and It seems to give me another error:Parse error: parse error, unexpected T_STRING, expecting ';' on line 7
@Peter - Try changing that to this: for ($i = 0; $node->nodeType != XML_TEXT_NODE)
karim79
this code is rubbish, now it is error on this line, unexpected t_string: foreach($cNodes as $cNode)
+4  A: 

The basic problem is, it's rubbish.

It looks like the code was originally supposed to be a formatter for displaying XML nodes in a debugging form. But it has been irretrievably mangled.

There are clearly characters missing — the unterminated string literals, the line “for ($i = 0; $i nodeType != XML_TEXT_NODE)” (which is nonsensical and looks like two different lines joined together)...

You would be better off saying what you actually want to do and writing the code than trying to execute random broken “code on the internet” without understanding what it does.

bobince
A: 

If all you want to do is to prettify the output DOMDocument can help you with that.
Set the DOMDocument object to "throw away" unnecessary whitespaces and have it format the output.

$doc = new DOMDocument;
$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;
$doc->loadHTMLFile('http://www.google.se');
echo $doc->savexml();
VolkerK