xml_parse example



        $this->_target = $locale;

        $encoding = $this->_findEncoding($filename);
        $this->_file = xml_parser_create($encoding);
        xml_set_object($this->_file, $this);
        xml_parser_set_option($this->_file, XML_OPTION_CASE_FOLDING, 0);
        xml_set_element_handler($this->_file, "_startElement", "_endElement");
        xml_set_character_data_handler($this->_file, "_contentElement");

        if (!xml_parse($this->_file, file_get_contents($filename))) {
            $ex = sprintf('XML error: %s at line %d of file %s',
                          xml_error_string(xml_get_error_code($this->_file)),
                          xml_get_current_line_number($this->_file),
                          $filename);
            xml_parser_free($this->_file);
            throw new Zend_Translate_Exception($ex);
        }

        return $this->_data;
    }

    
if (!is_readable($filename)) {
            throw new Zend_Translate_Exception('Translation file \'' . $filename . '\' is not readable.');
        }

        $encoding    = $this->_findEncoding($filename);
        $this->_file = xml_parser_create($encoding);
        xml_set_object($this->_file, $this);
        xml_parser_set_option($this->_file, XML_OPTION_CASE_FOLDING, 0);
        xml_set_element_handler($this->_file, "_startElement", "_endElement");
        xml_set_character_data_handler($this->_file, "_contentElement");

        if (!xml_parse($this->_file, file_get_contents($filename))) {
            $ex = sprintf('XML error: %s at line %d of file %s',
                          xml_error_string(xml_get_error_code($this->_file)),
                          xml_get_current_line_number($this->_file),
                          $filename);
            xml_parser_free($this->_file);
            throw new Zend_Translate_Exception($ex);
        }

        return $this->_data;
    }

    
if (!is_readable($filename)) {
            throw new Zend_Translate_Exception('Translation file \'' . $filename . '\' is not readable.');
        }

        $encoding = $this->_findEncoding($filename);
        $this->_file = xml_parser_create($encoding);
        xml_set_object($this->_file, $this);
        xml_parser_set_option($this->_file, XML_OPTION_CASE_FOLDING, 0);
        xml_set_element_handler($this->_file, "_startElement", "_endElement");
        xml_set_character_data_handler($this->_file, "_contentElement");

        if (!xml_parse($this->_file, file_get_contents($filename))) {
            $ex = sprintf('XML error: %s at line %d of file %s',
                          xml_error_string(xml_get_error_code($this->_file)),
                          xml_get_current_line_number($this->_file),
                          $filename);
            xml_parser_free($this->_file);
            throw new Zend_Translate_Exception($ex);
        }

        return $this->_data;
    }

    
$final = false;

        do {
            if (strlen($this->message) <= $chunk_size) {
                $final = true;
            }

            $part = substr($this->message, 0, $chunk_size);
            $this->message = substr($this->message, $chunk_size);

            if (!xml_parse($this->_parser, $part$final)) {
                xml_parser_free($this->_parser);
                unset($this->_parser);
                return false;
            }

            if ($final) {
                break;
            }
        } while (true);

        xml_parser_free($this->_parser);
        
xml_set_start_namespace_decl_handler($parser, "start_ns");
        xml_set_end_namespace_decl_handler($parser, "end_ns");

        $this->content = '';

        $ret = true;

        $fp = fopen($this->FILE, "r");
        while ($data = fread($fp, 4096)) {
            if($this->debug) $this->content .= $data;

            if(!xml_parse($parser$datafeof($fp))) {
                /* translators: 1: Error message, 2: Line number. */
                trigger_error(sprintf(__('XML Error: %1$s at line %2$s')."\n",
                    xml_error_string(xml_get_error_code($parser)),
                    xml_get_current_line_number($parser)));
                $ret = false;
                break;
            }
        }
        fclose($fp);

        xml_parser_free($parser);
        
$this->parser = $parser;

        # pass in parser, and a reference to this object         # set up handlers         #         xml_set_object( $this->parser, $this );
        xml_set_element_handler($this->parser,
                'feed_start_element', 'feed_end_element' );

        xml_set_character_data_handler( $this->parser, 'feed_cdata' );

        $status = xml_parse( $this->parser, $source );

        if ($status ) {
            $errorcode = xml_get_error_code( $this->parser );
            if ( $errorcode != XML_ERROR_NONE ) {
                $xml_error = xml_error_string( $errorcode );
                $error_line = xml_get_current_line_number($this->parser);
                $error_col = xml_get_current_column_number($this->parser);
                $errormsg = "$xml_error at line $error_line, column $error_col";

                $this->error( $errormsg );
            }
        }
if (isset($options['useId'])) {
            $this->_useId = (boolean) $options['useId'];
        }

        $encoding = $this->_findEncoding($filename);
        $this->_file = xml_parser_create($encoding);
        xml_set_object($this->_file, $this);
        xml_parser_set_option($this->_file, XML_OPTION_CASE_FOLDING, 0);
        xml_set_element_handler($this->_file, "_startElement", "_endElement");
        xml_set_character_data_handler($this->_file, "_contentElement");

        if (!xml_parse($this->_file, file_get_contents($filename))) {
            $ex = sprintf('XML error: %s at line %d of file %s',
                          xml_error_string(xml_get_error_code($this->_file)),
                          xml_get_current_line_number($this->_file),
                          $filename);
            xml_parser_free($this->_file);
            throw new Zend_Translate_Exception($ex);
        }

        return $this->_data;
    }

    
$this->_useId = true;
        }

        $encoding      = $this->_findEncoding($filename);
        $this->_target = $locale;
        $this->_file   = xml_parser_create($encoding);
        xml_set_object($this->_file, $this);
        xml_parser_set_option($this->_file, XML_OPTION_CASE_FOLDING, 0);
        xml_set_element_handler($this->_file, "_startElement", "_endElement");
        xml_set_character_data_handler($this->_file, "_contentElement");

        if (!xml_parse($this->_file, file_get_contents($filename))) {
            $ex = sprintf('XML error: %s at line %d of file %s',
                          xml_error_string(xml_get_error_code($this->_file)),
                          xml_get_current_line_number($this->_file),
                          $filename);
            xml_parser_free($this->_file);
            throw new Zend_Translate_Exception($ex);
        }

        return $this->_data;
    }

    
if ( ! str_contains( $data, '<' ) && ! str_contains( $data, '&' ) ) {
        return array( 'text', $data );
    }

    if ( ! function_exists( 'xml_parser_create' ) ) {
        trigger_error( __( "PHP's XML extension is not available. Please contact your hosting provider to enable PHP's XML extension." ) );

        return array( 'html', "<![CDATA[$data]]>" );
    }

    $parser = xml_parser_create();
    xml_parse( $parser, '<div>' . $data . '</div>', true );
    $code = xml_get_error_code( $parser );
    xml_parser_free( $parser );
    unset( $parser );

    if ( ! $code ) {
        if ( ! str_contains( $data, '<' ) ) {
            return array( 'text', $data );
        } else {
            $data = "<div xmlns='http://www.w3.org/1999/xhtml'>$data</div>";
            return array( 'xhtml', $data );
        }
    }
// Create an XML parser. if ( ! function_exists( 'xml_parser_create' ) ) {
    trigger_error( __( "PHP's XML extension is not available. Please contact your hosting provider to enable PHP's XML extension." ) );
    wp_die( __( "PHP's XML extension is not available. Please contact your hosting provider to enable PHP's XML extension." ) );
}

$xml_parser = xml_parser_create();

// Set the functions to handle opening and closing tags. xml_set_element_handler( $xml_parser, 'startElement', 'endElement' );

if ( ! xml_parse( $xml_parser$opml, true ) ) {
    printf(
        /* translators: 1: Error message, 2: Line number. */
        __( 'XML Error: %1$s at line %2$s' ),
        xml_error_string( xml_get_error_code( $xml_parser ) ),
        xml_get_current_line_number( $xml_parser )
    );
}

// Free up memory used by the XML parser. xml_parser_free( $xml_parser );
unset( $xml_parser );
// Parse!             $wrapper = @is_writable(sys_get_temp_dir()) ? 'php://temp' : 'php://memory';
            if (($stream = fopen($wrapper, 'r+')) &&
                fwrite($stream$data) &&
                rewind($stream))
            {
                //Parse by chunks not to use too much memory                 do
                {
                    $stream_data = fread($stream, 1048576);
                    if (!xml_parse($xml$stream_data === false ? '' : $stream_datafeof($stream)))
                    {
                        $this->error_code = xml_get_error_code($xml);
                        $this->error_string = xml_error_string($this->error_code);
                        $return = false;
                        break;
                    }
                } while (!feof($stream));
                fclose($stream);
            }
            else
            {
                
Home | Imprint | This part of the site doesn't use cookies.