simple snippets

A simple and easy to use PHP XML parser

Tagged xml, simple, parser, php  Languages php

The PHP XML parser:

class XML
{
    static function parse($data, $handler, $encoding = "UTF-8")
    {
        $parser = xml_parser_create($encoding);

        xml_set_object($parser, $handler);
        
        xml_set_element_handler($parser,
            array(&$handler, 'start'),
            array(&$handler, 'end')
        );
            
        xml_set_character_data_handler(
            $parser,
            array($handler, 'content')
        );
            
        $result = xml_parse($parser, $data);

        if(!$result)
        {
            $error_string = xml_error_string(xml_get_error_code($parser));
            $error_line   = xml_get_current_line_number($parser);
            $error_column = xml_get_current_column_number($parser);
            
            $message = sprintf("XML error '%s' at line %d column %d", $error_string, $error_line, $error_column);
            
            throw new Exception($message);
        }

        xml_parser_free($parser);
    }
}

A result handler:

class ResultHandler
{
    var $tag;

    function start ($parser, $tagName, $attributes = null)
    {
        echo "start";
        $this->tag .= $tagName; # Use .= to work around bug...
    }

    function end ($parser, $tagName)
    {
        echo "end";
        $this->tag = null;

    }

    function content ($parser, $content)
    {
        echo "$this->tag: $content" ;
    }
}

Then in your code:

$xml = "<a>bah</a>";
XML::parse($xml, new ResultHandler());

Note that HTML/XML entities are considered to be tags by PHP's XML parser, so your start tag handler will be called three times for this tag, once for "really", once for "&" and once for " bad parser":

<data>really &amp;  bad parser</data>

I guess this is a bug... You can

Really simple hover menus with jQuery

Tagged menu, jquery, simple, hover  Languages javascript

I got the inspiration for this simple menu from this blog post. It's based on the hover intent jQuery plugin by Brian Cherne.

The requirements:

Hide menu on mouse out:

Categories

Display menu on mouse over:

Categories

-Edit

-Delete

The HTML

<div id="categories-menu" class="hover-menu">
  <h2>Categories</h2>
  <ul class="actions no-style" style="display: none">
    <li><a href="">Add</a></li>
    <li><a href="">Edit</a></li>
    <li><a href="">Delete</a></li>
  </ul>
</div>

The JavaScript

<script type="text/javascript">
$(document).ready(function() {

  function show() {
    var menu = $(this);
    menu.children(".actions").slideDown();
  }
 
  function hide() { 
    var menu = $(this);
    menu.children(".actions").slideUp();
  }

  $(".hover-menu").hoverIntent({
    sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)
    interval: 50,   // number = milliseconds for onMouseOver polling interval
    over: show,     // function = onMouseOver callback (required)
    timeout: 300,   // number = milliseconds delay before onMouseOut
    out: hide       // function = onMouseOut callback (required)
  });
 
});
</script>

Positioning the hover menu over the content

The above example will show the menu inline with the content, effectively pushing it down. To avoid that, use this HTML:

<div id="categories-menu" class="hover-menu" style="position: relative">
  <h2>Categories</h2>
  <ul class="actions no-style" style="position: absolute; background: white; display: none;">
    <li><a href="" id="edit-mode">Edit mode</a></li>
    <li><a href="" class="last">Add</a></li>
  </ul>
</div>

hoverIntent alternative

See this snippet for an alternative solution.