Location: PHPKode > scripts > Lightning Notes > ln_distro/ln_distro/admin/rdf/classes/note_xml_parser.php
<?php

# XML Parser

class XMLParser
{
# Handle auf XML-Parser
# @var object
var $parser;

# CASE_FOLDING ja oder nein
# @var boolean
var $case_folding = TRUE;

# XML-Daten
# @var array
var $data = array();

# Aktuell geparstes XML-Tag
# @var string
var $current_tag = '';

# Zaehler fuer 'ITEM'
# @var integer
var $item_count = 0;

/*
# Flag auf 'data'
# @var boolean
var $data_flag = false; // <<<<----------------------------------
*/

# Flag auf 'ITEM'
# @var boolean
var $item_flag = false;

# Ausgabe - Optionen // <<<<----------------------------------
# @var array
var $options = array('DataDesc' => false,
                     'ItemDesc'    => TRUE,
                    );

# Ausgabe - Styles // <<<<----------------------------------
# @var array
var $styles = array('maxItem'       => 15,
                    'ItemTitle'     => '',
                    'ItemDesc'      => '',
                    );


/*
# div-Ausgabe der Titel
# @param   string  $file  URL des rss/rdf-files
# @return  string  $out   HTML-Output

function parseXML($file)  {

    $this->getXMLData($file);

    if ($this->styles['maxItem'] < $this->item_count) {
        $this->item_count = $this->styles['maxItem'];
    }
        // Items
        for ($i = 1; $i <= $this->item_count; $i++) {
            if (isset($this->data['ITEM'][$i]['TITLE']) && !empty($this->data['ITEM'][$i]['TITLE'])) {
                $out .= "<div class=\"item\">\n";
                    $out .='<h1><a href="'.$this->data['ITEM'][$i]['URL'].'">'.$this->data['ITEM'][$i]['TITLE'].'</a></h1>';
                    $out .= "\n";
                    
                    if (isset($this->data['ITEM'][$i]['SUBTITLE']) && !empty($this->data['ITEM'][$i]['SUBTITLE'])) {
                        $out .='<h2>'.$this->data['ITEM'][$i]['SUBTITLE'].'</h2>';
                        $out .= "\n";
                    }
    
                    if (isset($this->data['ITEM'][$i]['SUBSUBTITLE']) && !empty($this->data['ITEM'][$i]['SUBSUBTITLE'])) {
                        $out .='<h3>'.$this->data['ITEM'][$i]['SUBSUBTITLE'].'</h3>';
                        $out .= "\n";
                    }
    
                    if ($this->options['ItemDesc'] == true && isset($this->data['ITEM'][$i]['DESCRIPTION']) && !empty($this->data['ITEM'][$i]['DESCRIPTION'])) {
                        $out .= '<p>'.$this->data['ITEM'][$i]['DESCRIPTION'].'</p>';
                        $out .= "\n";
                    }
    
                    if (isset($this->data['ITEM'][$i]['AUTHOR']) && isset($this->data['ITEM'][$i]['DATE'])) {
                        $out .='<p>'.$this->data['ITEM'][$i]['AUTHOR'].', '.$this->data['ITEM'][$i]['DATE'].'</p>';
                        $out .= "\n";
                    }
                $out .= "</div>\n";
            }
        }
    return $out;
}


# li-Ausgabe der einzelnen Titel
# @param   string  $file  URL des xml-files
# @return  string  $out   HTML-Output


function parseLiXML($file) {
    $this->getXMLData($file);

    if ($this->styles['maxItem'] < $this->item_count) {
        $this->item_count = $this->styles['maxItem'];
    }
    $out .= "<ul>\n";
        // Items
        for ($i = 1; $i <= $this->item_count; $i++) {
            if (isset($this->data['ITEM'][$i]['TITLE']) && !empty($this->data['ITEM'][$i]['TITLE'])) {
                $out .= "<li>";
                $out .='<a href="'.$this->data['ITEM'][$i]['URL'].'" title="'.$this->data['ITEM'][$i]['AUTHOR'].', '.$this->data['ITEM'][$i]['DATE'].'"><strong>'.$this->data['ITEM'][$i]['TITLE'].'</strong></a>';
                
                if (isset($this->data['ITEM'][$i]['SUBTITLE']) && !empty($this->data['ITEM'][$i]['SUBTITLE'])) {
                    $out .='<br />'.$this->data['ITEM'][$i]['SUBTITLE'];
                } elseif (isset($this->data['ITEM'][$i]['DESCRIPTION']) && !empty($this->data['ITEM'][$i]['DESCRIPTION'])) {
                    $out .='<br />'.$this->data['ITEM'][$i]['DESCRIPTION'];
                }
            $out .= "</li>\n";
            }
        }
    $out .= "</ul>";
    return $out;
}
*/

# Ausgabeoptionen
# @param   array   $arr  Ausgabe-Optionen
# @return  void

function OutputOptions($arr) {
    for (reset($arr); list($k, $v) = each($arr);) {
        if (isset($this->options[$k])) {
            $this->options[$k] = $v;
        }
        else {
            $this->error('Unbekannte Output-Option <b>'.$k.'</b>');
        }
    }
}


# CSS Styles usw. fuer einzelne Ausgabebereiche
# @param   array   $arr  Ausgabe-Optionen
# @return  void

function OutputStyles($arr) {
    for (reset($arr); list($k, $v) = each($arr);) {
        if (isset($this->styles[$k])) {
            $this->styles[$k] = $v;
        }
        else {
            $this->error('Unbekanntes Output-Style <b>'.$k.'</b>');
        }
    }
}


# schreibt Channel in assoziatives Array
# @param   string   $file  URL des rss/rdf-files
# @return  array    $data  assoziatives Array mit den Channel-Daten

function getXMLData($file)
{
    $this->set_parser();
    $this->xml_file($file);
    return $this->data;
}


# Event-Handler fuer oeffnende Tags
# @param  string  $parser     Handle auf XML-Parser
# @param  string  $tag         Name des XML-Tags
# @param  array   $attribute  Tag-Attribute

function startElement($parser, $tag, $attribute) {

$this->current_tag = $this->StringToUpper($tag);

    if ($this->StringToUpper($tag) == 'ENTRY') {
        $this->item_flag = true;
        $this->item_count++;
    }
}


# Event-Handler fuer schliessende Tags
# @param string  $parser  Handle auf XML-Parser
# @param string  $tag     Name des XML-Tags

function endElement($parser, $tag) {

    if ($this->StringToUpper($tag) == 'ENTRY') {
        $this->item_flag = false;
    }
}


# Event-Handler fuer XML-Daten
# @param string $parser   Handle auf XML-Parser
# @param string $cdata    XML-Daten

function getCharacterData($parser, $cdata) {

    if ($this->item_flag == true) {
        if ($this->current_tag != 'ENTRY') {
            if (!isset($this->data['ENTRY'][$this->item_count][$this->current_tag])) {
                $this->data['ENTRY'][$this->item_count][$this->current_tag] = '';
            }
            $this->data['ENTRY'][$this->item_count][$this->current_tag] .= trim($cdata);
        }
    }
}


# Liest Inhalt aus XML-Datei ein und uebergibt ihn an den Parser
# @param  string   $file  Dateiname oder Pfad zur Datei

function xml_file($file) {

    if (!($fp = @fopen($file, "r")))
        $this->error("Kann XML-Datei <b>".$file."</b> nicht öffnen");

    while($data = fread($fp, 4096))
    {
    if (!(xml_parse($this->parser,$data)))
        $this->error("XML-Output: ".xml_error_string(xml_get_error_code($this->parser)));
    }

    xml_parser_free($this->parser);
}


# Erzeugen eines neuen XML-Parsers
# @param  void

function set_parser() {
    $this->parser = xml_parser_create();
    xml_set_object($this->parser, &$this);
    xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, $this->case_folding);
    xml_set_element_handler($this->parser,"startElement","endElement");
    xml_set_character_data_handler($this->parser,"getCharacterData");
}


# wandelt Tagnamen in Grossbuchstaben um
# @param  string   $tagname  Name des Tags
# @return string   $tagname  umgewandeltes Tag

function StringToUpper($tagname) {

    if ($this->case_folding)
        return strtoupper($tagname);
    else
        return $tagname;
}


# Gibt Fehlermeldung aus und bricht Script ab
# @param  string   $msg  Text der Fehlermeldung

function error($msg) {
    die(printf("Fehler: %s", $msg));
}

} // end of class

?>
Return current item: Lightning Notes