WEB php/xml parser

Discussion in 'OT Technology' started by burn__, Oct 13, 2008.

  1. burn__

    burn__ New Member

    Joined:
    Mar 21, 2006
    Messages:
    10,673
    Likes Received:
    0
    found a tutorial for a php4 xml parser and it looks perfect for what i need it for. problem is that it breaks due to something missing and i cannot find anything on google or the actual blog where it was posted as to why its not working.

    Code:
    Parse error: parse error, unexpected $ in /home/content/x/x/x/xxxxxxxx/html/war/rss.php on line 52
    
    i dont see any missing semi colons, parenthesis, brackets, or anything. been trying to figure out what the problem is for about 2 hours but it looks like everything is there thats suppose to be there :dunno:


    anyone see anything thats missing?

    Code:
    <?php
    	if (! ($xmlparser = xml_parser_create()) )
    	{
    	die ("Cannot create parser");
    	}
    
    	$current = "";
    	function start_tag($parser, $name, $attribs) {
    	   global $current;
    	   $current = $name;
    	   if ($name == "IMAGEINFO") { echo '<table border="1" width="50%">'; }
    	   if ($name == "IMAGE") { echo '<tr><td>'; }
    	   if ($name == "FILENAME") { echo '<img src="'; }
    	   if ($name == "NAME") { echo '</div/></td></tr><tr><td><div align="center">'; }
    	   if ($name == "SIZE") {
    	   if (is_array($attribs)) {
    		  while(list($key,$val) = each($attribs)) {
    			echo (strtolower($key).'="'.$val.'"');
    		  }
    	   }
    	}
    
    	function end_tag($parser, $name) {
    	   if ($name == "NAME") { echo "</div></td></tr>"; }
    	   if ($name == "FILENAME") { echo " />"; }
    	   if ($name == "IMAGEINFO") { echo "</table>"; }
    	}
    	
    	xml_set_element_handler($xmlparser, "start_tag", "end_tag");
    
    	function tag_contents($parser, $data) {
    	   global $current;
    	   if ($current == "FILENAME") { echo $data; }
    	   if ($current == "NAME") { echo $data; }
    	}
    
    	xml_set_character_data_handler($xmlparser, "tag_contents");
    
    	$filename = "sample.xml";
    	if (!($fp = fopen($filename, "r"))) { die("cannot open ".$filename); }
    	
    	while ($data = fread($fp, 4096)){
    	   $data=eregi_replace(">"."[[:space:]]+"."< ",">< ",$data);
    	   if (!xml_parse($xmlparser, $data, feof($fp))) {
    		  $reason = xml_error_string(xml_get_error_code($xmlparser));
    		  $reason .= xml_get_current_line_number($xmlparser);
    		  die($reason);
    	   }
    	}
    	xml_parser_free($xmlparser);
    
    ?> 
    
    and here's the xml file it had me create

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <imageinfo>
        <image>
    <filename>AmericanPie.jpg</filename>
    <size width="300" height="300" />
    <name>Mmm...Pie</name>
    </image>
    <image>
    <filename>Cantaloupe.jpg</filename>
    <size width="300" height="300" />
    <name>Cantaloupe</name>
    </image>
    <image>
    <filename>CitrusSlices.jpg</filename>
    <size width="300" height="300" />
    <name>Citrus For Summer</name>
    </image>
    </imageinfo>
    
     
  2. fishbulb

    fishbulb Active Member

    Joined:
    Oct 29, 2001
    Messages:
    6,848
    Likes Received:
    4
    Location:
    md
    Just pasting the code into my editor, it looks like the start_tag function doesn't have a closing bracket.
     
  3. whup

    whup I wish you had children and.. so that I could step

    Joined:
    Feb 12, 2007
    Messages:
    1,603
    Likes Received:
    0
    Yeah you're missing the closing bracket for the if statement in start_tag

    If you're using 1-line conditional statements, I recommend you drop the curly braces.

    Also, make sure you indent consistently and you'll avoid these errors.

    I like to have my matching braces on their own line, because they then line up column-wise and this makes it very easy to follow. This takes up a bit more space but it adds clarity.

    If you've got too much nesting going on, it means you can look at your logic a bit better to reduce it, as I did here with the &&

    Code:
    function start_tag($parser, $name, $attribs)
    {
        global $current;
        $current = $name;
        if ($name == "IMAGEINFO") echo '<table border="1" width="50%">';
        if ($name == "IMAGE") echo '<tr><td>';
        if ($name == "FILENAME") echo '<img src="';
        if ($name == "NAME") echo '</div/></td></tr><tr><td><div align="center">';
        if ($name == "SIZE" && is_array($attribs))
        {
            while(list($key,$val) = each($attribs))
            {
                echo (strtolower($key).'="'.$val.'"');
            }
        }
    }
    
    Really, this code could be in a switch statement.
     
  4. drpepper

    drpepper Active Member

    Joined:
    Nov 13, 2006
    Messages:
    38,076
    Likes Received:
    2
    Location:
    San Antonio
    what editor you use?
     
  5. burn__

    burn__ New Member

    Joined:
    Mar 21, 2006
    Messages:
    10,673
    Likes Received:
    0
    works now! :bigthumb:

    i havent actively written code for several years and i thought i could just jump back into it. now i know why i stopped, get confusing extremely fast.
     
  6. burn__

    burn__ New Member

    Joined:
    Mar 21, 2006
    Messages:
    10,673
    Likes Received:
    0
    :fawk:
     
  7. fishbulb

    fishbulb Active Member

    Joined:
    Oct 29, 2001
    Messages:
    6,848
    Likes Received:
    4
    Location:
    md
    PSPad, nothing fancy, but it's free and great for quick edits and has an ftp client
     

Share This Page