Why does this not parse xml?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • npm
    New Member
    • Apr 2007
    • 57

    Why does this not parse xml?

    Here's the sample xml:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <menu>
    	<entry>
    		<action>
    			Test
    		</action>
    		<title>
    			Test Menu Item 1
    		</title>
    	</entry>
    	<entry>
    		<action>
    			Test 2
    		</action>
    		<title>
    			Test Menu Item 2
    		</title>
    	</entry>
    </menu>
    And here's the php code:
    Code:
    <?php
    $dom = new DOMDocument();
    $dom->load("menu.xml");
    $menu = new domxpath($dom);
    foreach($menu->query("//menu/entry") as $entry) {
    	echo "Action is: " . $menu->query('.//action',$entry)->item(0)->nodeValue;
    	echo "Title is: " . $menu->query('.//title',$entry)->item(0)->nodeValue;
    }
    ?>
    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!
  • HaLo2FrEeEk
    Contributor
    • Feb 2007
    • 404

    #2
    Try SimpleXML:

    Comment

    • code green
      Recognized Expert Top Contributor
      • Mar 2007
      • 1726

      #3
      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

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Code Green, I don't see it?

        Comment

        • npm
          New Member
          • Apr 2007
          • 57

          #5
          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.

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            I'm struggling to spot the syntax error, also.

            And you're using PHP4? Seriously? It's 2011!

            Comment

            • npm
              New Member
              • Apr 2007
              • 57

              #7
              I know...

              I don't see where the error is AND it's been almost 7 years since 5.0.0 was released!

              Comment

              • code green
                Recognized Expert Top Contributor
                • Mar 2007
                • 1726

                #8
                Can I stop what seems to be a misunderstandin g.
                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

                Comment

                • npm
                  New Member
                  • Apr 2007
                  • 57

                  #9
                  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?

                  Comment

                  • npm
                    New Member
                    • Apr 2007
                    • 57

                    #10
                    Sorry, just for clarification, here's a visual of what I changed it to:
                    Code:
                    <?php
                    $dom = new DOMDocument();
                    $dom->load("xml.xml");
                    $menu = new DOMXPath($dom);
                    foreach($menu->query("//menu/entry") as $entry) {
                    	print "Action is: " . $menu->query('.//action',$entry)->item(0)->nodeValue;
                    	print "Title is: " . $menu->query('.//title',$entry)->item(0)->nodeValue;
                    }
                    ?>

                    Comment

                    • HaLo2FrEeEk
                      Contributor
                      • Feb 2007
                      • 404

                      #11
                      Try creating a new variable:

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

                      Now do print_r($entrie s). 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.

                      Comment

                      • npm
                        New Member
                        • Apr 2007
                        • 57

                        #12
                        Just to make sure...change it to, and run the following?
                        Code:
                        <?php
                        $dom = new DOMDocument();
                        $dom->load("xml.xml");
                        $menu = new DOMXPath($dom);
                        $entries = $menu->query("//menu/entry");
                        print_r($entries);
                        ?>

                        Comment

                        • Markus
                          Recognized Expert Expert
                          • Jun 2007
                          • 6092

                          #13
                          I'm going to compile the same PHP version and see if I get the same error.

                          Comment

                          • npm
                            New Member
                            • Apr 2007
                            • 57

                            #14
                            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?

                            Comment

                            • HaLo2FrEeEk
                              Contributor
                              • Feb 2007
                              • 404

                              #15
                              You didn't have to completely remove the foreach loop, but yes, run that and see what output you get.

                              Comment

                              Working...