Loading:


    xml_parse

    int xml_parse ( resource $parser , string $dane [, bool $ostatni ] )

    xml_parse() parsuje dokument XML. Uchwyty dla skonfigurowanych zdarzeń są wywoływane odpowiednią ilość razy.


    Parametry

     

    parser

    Referencja do parsera XML, który ma zostać użyty.



    dane

    Fragment danych do parsowania. Dokument może być prawidłowo parsowany po kawałku za pomocą wielokrotnego wywoływania xml_parse() z nowymi danymi, dopóki podany jest parametr ostatni i posiada on wartość logiczną TRUE podczas parsowania ostatniej porcji danych.



    ostatni

    Jeśli jest podany i posiada wartość logiczną TRUE, dane są ostatnim kawałkiem wysłanych danych podczas tego parsowania.



    Zwracane wartości

    Zwraca 1 w przypadku powodzenia lub 0 w przypadku błędu.

    Dla parsowań zakończonych niepomyślnie można uzyskać informację o błędach za pomocą xml_get_error_code(), xml_error_string(), xml_get_current_line_number(), xml_get_current_column_number() i xml_get_current_byte_index().


    Informacja: Błędy encji są raportowane po zakończeniu przetwarzania danych, a więc tylko wtedy gdy ustawiony jest parametr ostatni i posiada on wartość TRUE.



    Napisz Artyku³

    Listing


    <?php

    class xx_xml {

        // XML parser variables
        var $parser;
        var $name;
        var $attr;
        var $data  = array();
        var $stack = array();
        var $keys;
        var $path;
     
        // either you pass url atau contents.
        // Use 'url' or 'contents' for the parameter
        var $type;

        // function with the default parameter value
        function xx_xml($url='http://www.opocot.com', $type='url') {
            $this->type = $type;
            $this->url  = $url;
            $this->parse();
        }
     
        // parse XML data
        function parse()
        {
            $data = '';
            $this->parser = xml_parser_create ("UTF-8");
            xml_set_object($this->parser, $this);
            xml_set_element_handler($this->parser, 'startXML', 'endXML');
            xml_set_character_data_handler($this->parser, 'charXML');

            xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);

            if ($this->type == 'url') {
                // if use type = 'url' now we open the XML with fopen
             
                if (!($fp = @fopen($this->url, 'rb'))) {
                    $this->error("Cannot open {$this->url}");
                }

                while (($data = fread($fp, 8192))) {
                    if (!xml_parse($this->parser, $data, feof($fp))) {
                        $this->error(sprintf('XML error at line %d column %d',
                        xml_get_current_line_number($this->parser),
                        xml_get_current_column_number($this->parser)));
                    }
                }
            } else if ($this->type == 'contents') {
                // Now we can pass the contents, maybe if you want
                // to use CURL, SOCK or other method.
                $lines = explode("\n",$this->url);
                foreach ($lines as $val) {
                    if (trim($val) == '')
                        continue;
                    $data = $val . "\n";
                    if (!xml_parse($this->parser, $data)) {
                        echo $data.'<br />';
                        $this->error(sprintf('XML error at line %d column %d',
                        xml_get_current_line_number($this->parser),
                        xml_get_current_column_number($this->parser)));
                    }
                }
            }
        }

        function startXML($parser, $name, $attr)    {
            $this->stack[$name] = array();
            $keys = '';
            $total = count($this->stack)-1;
            $i=0;
            foreach ($this->stack as $key => $val)    {
                if (count($this->stack) > 1) {
                    if ($total == $i)
                        $keys .= $key;
                    else
                        $keys .= $key . '|'; // The saparator
                }
                else
                    $keys .= $key;
                $i++;
            }
            if (array_key_exists($keys, $this->data))    {
                $this->data[$keys][] = $attr;
            }    else
                $this->data[$keys] = $attr;
            $this->keys = $keys;
        }

        function endXML($parser, $name)    {
            end($this->stack);
            if (key($this->stack) == $name)
                array_pop($this->stack);
        }

        function charXML($parser, $data)    {
            if (trim($data) != '')
                @$startFrom = count($this->data[$this->keys])-1; // fixes weird splitting (bug?)
                @$startFrom = $startFrom == -1 ? $startFrom = 0 : $startFrom;
                @$this->data[$this->keys]['data'][$startFrom] .= trim(str_replace("\n", '', $data));
        }

        function error($msg)    {
            echo "<div align=\"center\">
                <font color=\"red\"><b>Error: $msg</b></font>
                </div>"
    ;
            exit();
        }
    }

    ?>




    Dodano przez: igor
    Ranga: Administrator serwisu Punktów: 0
    Komentarze użytkowników
      • Tre¶æ komentarza
        Kod do komentarza (opcjonalnie)
        PHP JavaScript MySQL Smarty SQL HTML CSS ActionScript
        Autor
        Token
        token

         

         








    funkcje.net
    Wszelkie prawa zastrzeżone©. | Funkcje.net 2008-2025 v.1.5 | design: diviXdesign & rainbowcolors