473,500 Members | 1,664 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why does this not parse xml?

57 New Member
Here's the sample xml:
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <menu>
  3.     <entry>
  4.         <action>
  5.             Test
  6.         </action>
  7.         <title>
  8.             Test Menu Item 1
  9.         </title>
  10.     </entry>
  11.     <entry>
  12.         <action>
  13.             Test 2
  14.         </action>
  15.         <title>
  16.             Test Menu Item 2
  17.         </title>
  18.     </entry>
  19. </menu>
And here's the php code:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $dom = new DOMDocument();
  3. $dom->load("menu.xml");
  4. $menu = new domxpath($dom);
  5. foreach($menu->query("//menu/entry") as $entry) {
  6.     echo "Action is: " . $menu->query('.//action',$entry)->item(0)->nodeValue;
  7.     echo "Title is: " . $menu->query('.//title',$entry)->item(0)->nodeValue;
  8. }
  9. ?>
So why, when I view the page, do I get this error:

Parse error: parse error, expecting `','' or `';'' in /home/httpd/vhosts/csnradio.com/www/test/2.php on line 6

I'm really clueless as to why this would happen because there are tutorials all over the place that are quite similar to this.

Thanks in advance!
Jan 27 '11 #1
18 1577
code green
1,726 Recognized Expert Top Contributor
You have a basic syntax error, that is all.
The error mesage points to where the error is (possibly the line above), which is line 6.
It is not obvious in the code supplied
Jan 27 '11 #3
Markus
6,050 Recognized Expert Expert
Code Green, I don't see it?
Jan 27 '11 #4
npm
57 New Member
I can't use SimpleXML because the server is running PHP 4.1.2. If I'm not mistaken, one needs version 5 to use SimpleXML, right?

I can see that the error is expecting a comma or a semicolon, but I don't know enough about php to know where they would go. Especially because the code is basically copied straight from a tutorial from someone who most likely had it working on their end.
Jan 27 '11 #5
Markus
6,050 Recognized Expert Expert
I'm struggling to spot the syntax error, also.

And you're using PHP4? Seriously? It's 2011!
Jan 27 '11 #6
npm
57 New Member
I know...

I don't see where the error is AND it's been almost 7 years since 5.0.0 was released!
Jan 27 '11 #7
code green
1,726 Recognized Expert Top Contributor
Can I stop what seems to be a misunderstanding.
I don't see a syntax error. I don't even believe the error is in the code provided.

I thought npm was panicking because there was an xml parsing error.
I was trying to correct this assumption because the message is a php syntax error.

Now, what we need to see is the block of code, and 'line 6' pointing out.
Sorry for the confusion
Jan 28 '11 #8
npm
57 New Member
Would the error be a setting or something with the actual server or in that version of PHP (4.1.2)?

I just don't see where the "," or the ";" should go.

This might be something, but I don't know what:
I switched both of the "echo" commands in lines 6-7 to "print" and now I get this error:

Parse error: parse error in /home/httpd/vhosts/csnradio.com/www/test/2.php on line 6

The error is in the same line, but it's different, right?
Jan 28 '11 #9
npm
57 New Member
Sorry, just for clarification, here's a visual of what I changed it to:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $dom = new DOMDocument();
  3. $dom->load("xml.xml");
  4. $menu = new DOMXPath($dom);
  5. foreach($menu->query("//menu/entry") as $entry) {
  6.     print "Action is: " . $menu->query('.//action',$entry)->item(0)->nodeValue;
  7.     print "Title is: " . $menu->query('.//title',$entry)->item(0)->nodeValue;
  8. }
  9. ?>
Jan 28 '11 #10
HaLo2FrEeEk
404 Contributor
Try creating a new variable:

$entries = $menu->query("//menu/entry");

Now do print_r($entries). Does it print anything? If so, keep the new variable and replace $menu->query("//menu/entry") in your foreach with $entries.

Edit: comment out the entire for loop before doing print_r or the error might still happen.
Jan 28 '11 #11
npm
57 New Member
Just to make sure...change it to, and run the following?
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $dom = new DOMDocument();
  3. $dom->load("xml.xml");
  4. $menu = new DOMXPath($dom);
  5. $entries = $menu->query("//menu/entry");
  6. print_r($entries);
  7. ?>
Jan 28 '11 #12
Markus
6,050 Recognized Expert Expert
I'm going to compile the same PHP version and see if I get the same error.
Jan 28 '11 #13
npm
57 New Member
I ran the above code and got this:

Fatal error: Call to undefined function: load() in /home/httpd/vhosts/csnradio.com/www/test/2.php on line 3

Now what?
Jan 28 '11 #14
HaLo2FrEeEk
404 Contributor
You didn't have to completely remove the foreach loop, but yes, run that and see what output you get.
Jan 28 '11 #15
HaLo2FrEeEk
404 Contributor
Try creating a new variable:

$entries = $menu->query("//menu/entry");

Now do print_r($entries). Does it print anything? If so, keep the new variable and replace $menu->query("//menu/entry") in your foreach with $entries.
Jan 28 '11 #16
Markus
6,050 Recognized Expert Expert
HaLo, I think we might be forgiven for this, considering PHP4 is almost a million years old. ;)

npm, is there any particular reason you're still using PHP4?
Jan 28 '11 #17
npm
57 New Member
The server is from an outside company. I've contacted them about the possibility of an upgrade. Thank you all for your time, however in vain in turned out to be. :)
Jan 28 '11 #18
HaLo2FrEeEk
404 Contributor
Ask them if there's any way you can install your own custom PHP. I'm on Dreamhost and they let users install their own version of PHP, it's not difficult, just a few files to copy and some changes to the .htaccess file. Ok, it's a little more complicated than that, but not much.
Jan 28 '11 #19

Sign in to post your reply or Sign up for a free account.

Similar topics

22
872
by: Ram Laxman | last post by:
Hi all, I have a text file which have data in CSV format. "empno","phonenumber","wardnumber" 12345,2234353,1000202 12326,2243653,1000098 Iam a beginner of C/C++ programming. I don't know how to...
24
3124
by: | last post by:
Hi, I need to read a big CSV file, where different fields should be converted to different types, such as int, double, datetime, SqlMoney, etc. I have an array, which describes the fields and...
8
10387
by: Douglas Crockford | last post by:
There is a new version of JSON.parse in JavaScript. It is vastly faster and smaller than the previous version. It uses a single call to eval to do the conversion, guarded by a single regexp test to...
4
1866
by: fyleow | last post by:
I create a new Python file with the following using Wing IDE. import feedparser d = feedparser.parse("http://feedparser.org/docs/examples/atom10.xml") print d.feed.title I get this error when...
34
2923
by: priyanka | last post by:
Hi, I was wondering if we could parse or do something in the executable( whose source language was C). How can I use some scripting language like perl/python to find out the information about...
29
2867
by: gs | last post by:
let say I have to deal with various date format and I am give format string from one of the following dd/mm/yyyy mm/dd/yyyy dd/mmm/yyyy mmm/dd/yyyy dd/mm/yy mm/dd/yy dd/mmm/yy mmm/dd/yy
7
14114
by: Perks | last post by:
Hi. I am trying to find out if it is possible to open a pdf file from within PHP, and parse its contents in order to extract all form fieldnames that might have been previously setup within the...
5
64575
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C++ programming. FYI Although I have called...
1
63992
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C programming. FYI Although I have called this...
11
2682
by: MonkeeSage | last post by:
A quick question about how python parses a file into compiled bytecode. Does it parse the whole file into AST first and then compile the AST, or does it build and compile the AST on the fly as it...
0
7136
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7018
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7183
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6909
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5491
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4923
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3108
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
675
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
317
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.