fk: XML to array

Beitrag lesen

ich bin mal so nett aus meinem bastelfundus:

/**
* Diese Klasse erstellt ein array aus einem XML Datenstrom
*
* Ausgabe:
* [TAG1][TAG2][<ATTRIBUTES>] = Attribute des Tags(array)
* [TAG1][TAG2][<CDATA>] = Text
* oder XPATH
* [TAG1.TAG2.<ATTRIBUTES>] = Attribute des Tags(array)
* [TAG1.TAG2.<CDATA>] = Text
* oder gemischt
*
* Geistiges Eigentum von XXXXXXXXXXXXX - Internetprogrammierung - .
* Die Weitergabe ist nur mit diesem Kommentar erlaubt.
* Änderungen Vorbehalten
*
* @access       public
* @author       XXXXXXXXXXXXXXXX
* @copyright    XXXXXXXXXXXXXXXX - Internetprogrammierung - .
* @package      Utilities
* @version      0.1
*/
class xml2array {
    //
    var $Ausgabe = array();
    var $a = array();
    var $parser;
    var $Pfad = '';
    var $tags = array();
    //
    function xml2array() {
        echo('<!-- '.__CLASS__.'->'.__FUNCTION__.'('.')['.__LINE__.'] -->'."\r\n");
        //$this->parser = $this->xml_parser_create();
        $this->xml_parser_create('ISO-8859-1');
        xml_set_object($this->parser, &$this);
        xml_set_element_handler($this->parser, '_tag_open', '_tag_close');
        xml_set_character_data_handler($this->parser, '_cdata');
        xml_set_default_handler($this->parser, '_sonst');
        $this->a = &$this->Ausgabe;
    }
    //
    function &parse($data) {
        echo('<!-- '.__CLASS__.'->'.__FUNCTION__.'('.$data.')['.__LINE__.'] -->'."\r\n");
        $this->Ausgabe = array();
        $this->a = &$this->Ausgabe;
        $this->parser;
        $this->Pfad = '';
        $this->tags = array();
        if ($result = xml_parse($this->parser, $data)) {
            echo('<!-- '.__FILE__.'['.__LINE__.']'."\r\n");
            echo('return: '); print_r($this->Ausgabe); echo("\r\n");
            echo(__FILE__.'['.__LINE__.']'.' -->'."\r\n");
            return $this->Ausgabe;
        }
        else {
            return $this->parser;
        }
    }
    //
    function &xml_parser_create($Zeichensatz = NULL) {
        //echo('<!-- '.__CLASS__.'->'.__FUNCTION__.'('.')['.__LINE__.'] -->'."\r\n");
        return $this->parser = xml_parser_create($Zeichensatz);
    }
    //
    function &xml_parser_free($parser = NULL) {
        //echo('<!-- '.__CLASS__.'->'.__FUNCTION__.'('.$parser.')['.__LINE__.'] -->'."\r\n");
        if (isset($parser)) {
            return xml_parser_free($parser);
        }
        else {
            return xml_parser_free($this->parser);
        }
    }
    //
    function &xml_get_error_code($parser = NULL) {
        //echo('<!-- '.__CLASS__.'->'.__FUNCTION__.'('.$parser.')['.__LINE__.'] -->'."\r\n");
        if (isset($parser)) {
            return xml_get_error_code($parser);
        }
        else {
            return xml_get_error_code($this->parser);
        }
    }
    //
    function &xml_error_string($code = NULL) {
        //echo('<!-- '.__CLASS__.'->'.__FUNCTION__.'('.$code.')['.__LINE__.'] -->'."\r\n");
        if (isset($code)) {
            return xml_error_string($code);
        }
        else {
            return xml_error_string($this->xml_get_error_code());
        }
    }
    //
    function _tag_open($parser, $tag, $attributes) {
        //echo('<!-- '.__CLASS__.'->'.__FUNCTION__.'('.$parser.', '.$tag.', '.$attributes.')['.__LINE__.'] -->'."\r\n");
        $this->Pfad = join('.', $this->tags);
        if ($this->Pfad <> '') {
            $this->a = &$this->Ausgabe[$this->Pfad];
        }
        else {
            $this->a = &$this->Ausgabe;
        }
        if (!isset($this->a[$tag])) {
            $this->a[$tag] = array();
        }
        $i = count($this->a[$tag]);
        $this->a[$tag][$i] = array();
        $this->a[$tag][$i]['<ATTRIBUTES>'] = $attributes;
        $this->a[$tag][$i]['<CDATA>'] = '';
        $this->a[$tag.'['.$i.'].<ATTRIBUTES>'] = &$this->a[$tag][$i]['<ATTRIBUTES>'];
        $this->a[$tag.'['.$i.'].<CDATA>'] = &$this->a[$tag][$i]['<CDATA>'];
        $this->tags[count($this->tags)] = $tag.'['.$i.']';
        $this->Pfad = join('.', $this->tags);
        $this->Ausgabe[$this->Pfad] = &$this->a[$tag][$i];
        $this->a = &$this->Ausgabe[$this->Pfad];
        $this->Ausgabe[$this->Pfad.'.<ATTRIBUTES>'] = &$this->a['<ATTRIBUTES>'];
        $this->Ausgabe[$this->Pfad.'.<CDATA>'] = &$this->a['<CDATA>'];
    }
    //
    function _cdata($parser, $cdata) {
        //echo('<!-- '.__CLASS__.'->'.__FUNCTION__.'('.$parser.', '.$cdata.')['.__LINE__.'] -->'."\r\n");
        $this->a['<CDATA>'] .= $cdata;
    }
    //
    function _tag_close($parser, $tag) {
        //echo('<!-- '.__CLASS__.'->'.__FUNCTION__.'('.$parser.', '.$tag.')['.__LINE__.'] -->'."\r\n");
        unset ($this->tags[count($this->tags)-1]);
        $this->Pfad = join('.', $this->tags);
        $this->a = &$this->Ausgabe[$this->Pfad];
    }
    //
    function _sonst($parser, $sonst) {
        //echo('<!-- '.__CLASS__.'->'.__FUNCTION__.'('.$parser.', '.$sonst.')['.__LINE__.'] -->'."\r\n");
        return;
        echo('<!-- '.__FILE__.'['.__LINE__.']'."\r\n");
        echo('sonst: '); print_r($sonst); echo("\r\n");
        echo(__FILE__.'['.__LINE__.']'.' -->'."\r\n");
    }
    //
}

benutzung: (nur als beispiel)

unset($xml2array);
$xml2array = new xml2array();
echo('<!-- '.__FILE__.'['.__LINE__.'] -->'."\r\n");
$a = $xml2array->parse($UserCheck);
echo('<!-- '.__FILE__.'['.__LINE__.'] a: '.$a.' -->'."\r\n");
if (is_array($a)) {
    //echo('a: '); var_dump($a);
    $userdata = &$a['XICONTAINER'][0]['XIRESULT'][0]['RESULTUSERCHECK'][0];
    $userdata = &$a['XICONTAINER[0].XIRESULT[0].RESULTUSERCHECK[0]'];
    //echo('userdata: '); var_dump($userdata);
    echo('Auswahlstufe: '); echo($userdata['BOOLEAN[0].<CDATA>']); echo '  ';
}
else {
    //echo (xml_error_string(xml_get_error_code($a)));
    echo ($xml2array->xml_get_error_code());
    echo ': ';
    echo ($xml2array->xml_error_string($xml2array->xml_get_error_code()));
}