WXR_Parser_XML::parse PHP Method

parse() public method

public parse ( $file )
    function parse($file)
    {
        $this->wxr_version = $this->in_post = $this->cdata = $this->data = $this->sub_data = $this->in_tag = $this->in_sub_tag = false;
        $this->authors = $this->posts = $this->term = $this->category = $this->tag = array();
        $xml = xml_parser_create('UTF-8');
        xml_parser_set_option($xml, XML_OPTION_SKIP_WHITE, 1);
        xml_parser_set_option($xml, XML_OPTION_CASE_FOLDING, 0);
        xml_set_object($xml, $this);
        xml_set_character_data_handler($xml, 'cdata');
        xml_set_element_handler($xml, 'tag_open', 'tag_close');
        if (!xml_parse($xml, file_get_contents($file), true)) {
            $current_line = xml_get_current_line_number($xml);
            $current_column = xml_get_current_column_number($xml);
            $error_code = xml_get_error_code($xml);
            $error_string = xml_error_string($error_code);
            return new WP_Error('XML_parse_error', 'There was an error when reading this WXR file', array($current_line, $current_column, $error_string));
        }
        xml_parser_free($xml);
        if (!preg_match('/^\\d+\\.\\d+$/', $this->wxr_version)) {
            return new WP_Error('WXR_parse_error', __('This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer'));
        }
        return array('authors' => $this->authors, 'posts' => $this->posts, 'categories' => $this->category, 'tags' => $this->tag, 'terms' => $this->term, 'base_url' => $this->base_url, 'version' => $this->wxr_version);
    }

Usage Example

Example #1
0
 function parse($file)
 {
     // Attempt to use proper XML parsers first
     if (extension_loaded('simplexml')) {
         $parser = new WXR_Parser_SimpleXML();
         $result = $parser->parse($file);
         // If SimpleXML succeeds or this is an invalid WXR file then return the results
         return $result;
     } else {
         if (extension_loaded('xml')) {
             $parser = new WXR_Parser_XML();
             $result = $parser->parse($file);
             // If XMLParser succeeds or this is an invalid WXR file then return the results
             return $result;
         }
     }
     // use regular expressions if nothing else available or this is bad XML
     $parser = new WXR_Parser_Regex();
     return $parser->parse($file);
 }
All Usage Examples Of WXR_Parser_XML::parse