Page 1 of 1

new DOMDocument - parse error (Win7 U 64) 1.7.7 [SOLVED]

PostPosted: 08. December 2011 21:56
by Ikonn
Hi
I am using XAMPP 1.7.7 on Windows7 64-bit.
When using this code:

Code: Select all
<?php
     $doc = new DOMDocument('1.0', 'iso-8859-1');
...
...
...
?>

i get: Parse error: syntax error, unexpected T_VARIABLE in D:\xampp\htdocs\devel\domtest.php on line 2.

I got this code from the internet as I just started learning PHP.
I went to http://www.php.net/manual/en/domdocument.construct.php and it seems that i should not have any problems.
Could anyone shed some light on what might be the problem?

Re: new DOMDocument - parse error

PostPosted: 08. December 2011 22:06
by Altrea
Hi Ikonn,

your small sourcecode works on my environment as expected without any errors.
Can you show us the rest of your sourcecode?

best wishes,
Altrea

Re: new DOMDocument - parse error

PostPosted: 08. December 2011 22:17
by Ikonn
Of course:

Code: Select all
<?php
 $doc = new DOMDocument('1.0', 'iso-8859-1');
   
  $file = 'data.html';
 
  //load the html 
  $html = $doc->loadHTMLFile($file);

  //discard white space
  $doc->preserveWhiteSpace = false;

  //the table by its tag name
  $tables = $doc->getElementsByTagName('table');

  //get all rows from the table
  $rows = $tables->item(0)->getElementsByTagName('tr');

  // loop over the table rows
  foreach ($rows as $row)
  {
   // get each column by tag name
      $cols = $row->getElementsByTagName('td');
   // echo the values 
      echo $cols->item(0)->nodeValue.'';
      echo $cols->item(1)->nodeValue.'';
      echo $cols->item(2)->nodeValue;
    }

?>

Re: new DOMDocument - parse error

PostPosted: 08. December 2011 22:59
by Sharley
Ikonn wrote:I am using XAMPP 1.7.7 on Windows7 64-bit.
Hello, would you be so kind as to add your XAMPP version and Operating System to your profile:
viewtopic.php?f=16&t=48626
Thanks. :)

Re: new DOMDocument - parse error

PostPosted: 08. December 2011 23:09
by Ikonn
Done. :)

Re: new DOMDocument - parse error

PostPosted: 08. December 2011 23:18
by Altrea
Again, your sourcecode works perfectly (after i have created a dummy data.html file).

Which Editor do you use?
Which character encoding does the editor use?

Re: new DOMDocument - parse error

PostPosted: 08. December 2011 23:22
by Ikonn
I am using Notepad++ with ANSI encoding (default).
I will try other editor (NetBeans IDE) and let you know about outcome.

Re: new DOMDocument - parse error

PostPosted: 08. December 2011 23:28
by Sharley
Ikonn wrote:Done.
Thanks for that. 8)

Sorry it's no help but that line also works in my 1.7.7 environment using Note Tab Pro and Notepad++ both in ANSI.

Best wishes. :)

Re: new DOMDocument - parse error

PostPosted: 08. December 2011 23:34
by Ikonn
Altrea wrote:Again, your sourcecode works perfectly (after i have created a dummy data.html file).

Which Editor do you use?
Which character encoding does the editor use?


The problem was that pasting code into the Notepad++ included some special characters not visible inside Notepad++ itself.
In NetBeans i have not seen those characters as well but I got warnings inside editor about syntax problem.
After rectifying those problems code works perfect.
Lesson learned - use tools that are proven to work.

Thank you for your prompt help and apologies for not checking that myself. I would never suspect that would might be a problem.
Thank you again :!:

Re: !solved! new DOMDocument - parse error

PostPosted: 08. December 2011 23:44
by Altrea
I have never had problems with Notepad++ like unvisible characters.
What characters was it? Or was is something like a Byte-Order-Mark?

Re: new DOMDocument - parse error (Win7 U 64) 1.7.7 [SOLVED]

PostPosted: 09. December 2011 00:30
by Ikonn
I am not sure if I can explain. I think it will be easier for me to paste screenshots with non-printing characters enabled in both editors:
Notepad++
Image

NetBeans
Image

I am including those pictures because I have no idea what is "Byte-Order-Mark" :oops: and I am not entirely sure what specific character is causing those problems.
EDIT: If you are interested I can provide you with a link where I took code from.

Re: new DOMDocument - parse error (Win7 U 64) 1.7.7 [SOLVED]

PostPosted: 09. December 2011 00:39
by Sharley
Ikonn wrote:EDIT: If you are interested I can provide you with a link where I took code from.
That could also be helpful, thanks.

How you save the file in Notepad++ could be the issue and if you use All file (*) and manually add the file name and extension then that will usually save the file in the default encoding settings.

Re: new DOMDocument - parse error (Win7 U 64) 1.7.7 [SOLVED]

PostPosted: 09. December 2011 00:42
by Altrea
very interesting. looks like control characters or some kind of white space characters in front of each line used for indentation of each line :shock:

Re: new DOMDocument - parse error (Win7 U 64) 1.7.7 [SOLVED]

PostPosted: 09. December 2011 04:48
by Ikonn
The url is below:
http://techgossipz.blogspot.com/2010/02/how-to-parse-html-using-dom-with-php.html

I basically selected "view plain", then copy/pasted into existing, opened in Notepad++, .php file, then saved.
Then I spent 4 hrs looking for solution :wink:

Re: new DOMDocument - parse error (Win7 U 64) 1.7.7 [SOLVED]

PostPosted: 09. December 2011 05:01
by Sharley
When you do a copy and paste even selecting plain from the link you provided here is what you get
Code: Select all
&lt;?php
&nbsp; // new dom object
&nbsp; $dom = new DOMDocument();

&nbsp; //load the html
&nbsp; $html = $dom-&gt;loadHTMLFile(data.html);

&nbsp; //discard white space
&nbsp; $dom-&gt;preserveWhiteSpace = false;

&nbsp; //the table by its tag name

&nbsp; $tables = $dom-&gt;getElementsByTagName('table');

&nbsp; //get all rows from the table
&nbsp; $rows = $tables-&gt;item(0)-&gt;getElementsByTagName('tr');

&nbsp; // loop over the table rows
&nbsp; foreach ($rows as $row)
&nbsp; {
&nbsp;&nbsp; // get each column by tag name

&nbsp; &nbsp; &nbsp; $cols = $row-&gt;getElementsByTagName('td');
&nbsp;&nbsp; // echo the values&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo $cols-&gt;item(0)-&gt;nodeValue.'&lt;br /&gt;';
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo $cols-&gt;item(1)-&gt;nodeValue.'&lt;br /&gt;';

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo $cols-&gt;item(2)-&gt;nodeValue;
&nbsp;&nbsp;&nbsp; }

?&gt;
To find this out do a right click on the page and view the code - I can select the text I want to copy and then right click and view my selection only and that is what is in the code snippet above.

Those would be hidden in most editors but depending on your Save As setting they may well also be saved in to the file you create.

The web developer should have known better than to add those entities when posting php code snippets - it makes html looks nice but the repercussions for other codes are 4 hours of trying to sort out the developers mess. :wink: :)

So when you copy and paste from a web site page make sure that you are only copying only the code that you expect.

Good luck. :)