WXR_Parser

WordPress Importer class for managing parsing of WXR files.

Defined (1)

The class is defined in the following location(s).

/parsers.php  
  1. class WXR_Parser { 
  2. function parse( $file ) { 
  3. // Attempt to use proper XML parsers first 
  4. if ( extension_loaded( 'simplexml' ) ) { 
  5. $parser = new WXR_Parser_SimpleXML; 
  6. $result = $parser->parse( $file ); 
  7.  
  8. // If SimpleXML succeeds or this is an invalid WXR file then return the results 
  9. if ( ! is_wp_error( $result ) || 'SimpleXML_parse_error' != $result->get_error_code() ) 
  10. return $result; 
  11. } else if ( extension_loaded( 'xml' ) ) { 
  12. $parser = new WXR_Parser_XML; 
  13. $result = $parser->parse( $file ); 
  14.  
  15. // If XMLParser succeeds or this is an invalid WXR file then return the results 
  16. if ( ! is_wp_error( $result ) || 'XML_parse_error' != $result->get_error_code() ) 
  17. return $result; 
  18.  
  19. // We have a malformed XML file, so display the error and fallthrough to regex 
  20. if ( isset($result) && defined('IMPORT_DEBUG') && IMPORT_DEBUG ) { 
  21. echo '<pre>'; 
  22. if ( 'SimpleXML_parse_error' == $result->get_error_code() ) { 
  23. foreach ( $result->get_error_data() as $error ) 
  24. echo $error->line . ':' . $error->column . ' ' . esc_html( $error->message ) . "\n"; 
  25. } else if ( 'XML_parse_error' == $result->get_error_code() ) { 
  26. $error = $result->get_error_data(); 
  27. echo $error[0] . ':' . $error[1] . ' ' . esc_html( $error[2] ); 
  28. echo '</pre>'; 
  29. echo '<p><strong>' . __( 'There was an error when reading this WXR file', 'wordpress-importer' ) . '</strong><br />'; 
  30. echo __( 'Details are shown above. The importer will now try again with a different parser...', 'wordpress-importer' ) . '</p>'; 
  31.  
  32. // use regular expressions if nothing else available or this is bad XML 
  33. $parser = new WXR_Parser_Regex; 
  34. return $parser->parse( $file );