Location: PHPKode > projects > Mozilla To Web Bookmark Tools > moz2web-tools-0.1/xml_bookmark_class.php
<?
/*------------------------------------------------------------------------------
 | script name: xml_bookmark_class.php
 | script info: see README
 | version:     0.1 
 |     
 | created by:  Ralph Schindler
 | licensing:   GNU General Public Licence - 'feel free to f*ck w/ it'
 -----------------------------------------------------------------------------*/


class xml_bookmark {

    var $all_tags = array();
    var $parents = array();
    var $current_tag;
    var $current_attrs;
    var $last_popped;
    var $item_count = 0;
    var $current_depth = 0;
    var $last_depth = -1;

    function startElement($parser, $name, $attrs) {
        $this->current_attrs = $attrs;
        $this->current_tag = $name;
        array_push($this->all_tags, $name);
        $this->current_depth = count($this->all_tags);
    }

    function endElement($parser, $name) {
        $this->last_popped = array_pop($this->all_tags);
    }

    function cdataElement($parser, $cdata) {
        $cdata = trim($cdata);
        $all_tags_together = implode("_", $this->all_tags);
        
        if ( ( substr($all_tags_together, (-1 * strlen($this->current_tag) - 1) ) == ("_" . $this->current_tag) ) && ($cdata != "") ) {

            if ( $this->current_depth > $this->last_depth ) {
                $this->parent_id = $this->item_count;
                array_push($this->parents, $this->parent_id);
            } else if ( $this->current_depth < $this->last_depth ) {
                $diff =  $this->last_depth - $this->current_depth;
                for($i=1;$i<=$diff;$i++) array_pop($this->parents);
            }

            ++$this->item_count;
            $this->data[$this->item_count]->link_name = $cdata;
            $this->data[$this->item_count]->depth  = (count($this->all_tags)-1);
            $where_parent = count($this->parents) -1;
            $this->data[$this->item_count]->parent_id = $this->parents[$where_parent];
            $this->data[$this->item_count]->all_family = "|" . implode("|", $this->parents) . "|";

            foreach($this->current_attrs as $key_attr => $value_attr) {
                $key_attr = strtolower($key_attr);
                $this->data[$this->item_count]->{$key_attr} = $value_attr;
            }
            
            $this->last_depth = $this->current_depth;

        } else if ( strlen($cdata) > 0) {

            $this->data[$this->item_count]->description .= $cdata . "\n";

        }
        
    }


    function xml_bookmark($bookmark_data) {
    
        // clean up the data
        $bookmark_data = stristr($bookmark_data, "<dl>");
        $bookmark_data = eregi_replace("(<p>|<dt>|<hr>|<dd>|&quot;|&lt;|&gt;|&amp;)", "", $bookmark_data);
        $bookmark_data = eregi_replace("&", "&amp;", $bookmark_data);
       
        
        $this->xml_parser = xml_parser_create();
        
        xml_set_object($this->xml_parser,$this);
        xml_parser_set_option($this->xml_parser,XML_OPTION_CASE_FOLDING,0);
        xml_set_element_handler($this->xml_parser, "startElement", "endElement");
        xml_set_character_data_handler($this->xml_parser, "cdataElement");
        
        if (!xml_parse($this->xml_parser, $bookmark_data, TRUE))
            die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($this->xml_parser)), xml_get_current_line_number($this->xml_parser)));

        // cleanup       
        xml_parser_free($this->xml_parser);
        
        // remove stuff from the outgoing structure
        unset($this->all_tags, $this->current_depth, $this->current_tag, $this->last_popped, $this->xml_parser, $this->last_depth, $this->parents, $this->current_attrs, $this->parent_id);
    }


}






?>
Return current item: Mozilla To Web Bookmark Tools