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

Problems with the Windows version of XAMPP, questions, comments, and anything related.

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

Postby Ikonn » 08. December 2011 21:56

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?
Last edited by Ikonn on 08. December 2011 23:35, edited 1 time in total.
Ikonn
 
Posts: 27
Joined: 23. January 2011 03:30
Operating System: GNU/Linux Debian

Re: new DOMDocument - parse error

Postby Altrea » 08. December 2011 22:06

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
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11926
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64

Re: new DOMDocument - parse error

Postby Ikonn » 08. December 2011 22:17

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;
    }

?>
Ikonn
 
Posts: 27
Joined: 23. January 2011 03:30
Operating System: GNU/Linux Debian

Re: new DOMDocument - parse error

Postby Sharley » 08. December 2011 22:59

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. :)
User avatar
Sharley
AF Moderator
 
Posts: 3316
Joined: 03. October 2008 05:10
Location: Yeppoon, Australia Time Zone: GMT/UTC+10
Operating System: Win 7 Pro 32bit/XP Pro SP3

Re: new DOMDocument - parse error

Postby Ikonn » 08. December 2011 23:09

Done. :)
Ikonn
 
Posts: 27
Joined: 23. January 2011 03:30
Operating System: GNU/Linux Debian

Re: new DOMDocument - parse error

Postby Altrea » 08. December 2011 23:18

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?
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11926
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64

Re: new DOMDocument - parse error

Postby Ikonn » 08. December 2011 23:22

I am using Notepad++ with ANSI encoding (default).
I will try other editor (NetBeans IDE) and let you know about outcome.
Ikonn
 
Posts: 27
Joined: 23. January 2011 03:30
Operating System: GNU/Linux Debian

Re: new DOMDocument - parse error

Postby Sharley » 08. December 2011 23:28

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. :)
User avatar
Sharley
AF Moderator
 
Posts: 3316
Joined: 03. October 2008 05:10
Location: Yeppoon, Australia Time Zone: GMT/UTC+10
Operating System: Win 7 Pro 32bit/XP Pro SP3

Re: new DOMDocument - parse error

Postby Ikonn » 08. December 2011 23:34

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 :!:
Ikonn
 
Posts: 27
Joined: 23. January 2011 03:30
Operating System: GNU/Linux Debian

Re: !solved! new DOMDocument - parse error

Postby Altrea » 08. December 2011 23:44

I have never had problems with Notepad++ like unvisible characters.
What characters was it? Or was is something like a Byte-Order-Mark?
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11926
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64

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

Postby Ikonn » 09. December 2011 00:30

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.
Ikonn
 
Posts: 27
Joined: 23. January 2011 03:30
Operating System: GNU/Linux Debian

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

Postby Sharley » 09. December 2011 00:39

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.
User avatar
Sharley
AF Moderator
 
Posts: 3316
Joined: 03. October 2008 05:10
Location: Yeppoon, Australia Time Zone: GMT/UTC+10
Operating System: Win 7 Pro 32bit/XP Pro SP3

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

Postby Altrea » 09. December 2011 00:42

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:
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11926
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64

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

Postby Ikonn » 09. December 2011 04:48

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:
Ikonn
 
Posts: 27
Joined: 23. January 2011 03:30
Operating System: GNU/Linux Debian

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

Postby Sharley » 09. December 2011 05:01

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. :)
User avatar
Sharley
AF Moderator
 
Posts: 3316
Joined: 03. October 2008 05:10
Location: Yeppoon, Australia Time Zone: GMT/UTC+10
Operating System: Win 7 Pro 32bit/XP Pro SP3


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 130 guests