You are here

class SimpleXMLFIReader in Feed Import 7.3

SimpleXML Reader class, good for small-medium XML files.

Hierarchy

Expanded class hierarchy of SimpleXMLFIReader

2 string references to 'SimpleXMLFIReader'
FeedImport::getEmptyFeed in feed_import_base/inc/feed_import.inc
Gets a new empty feed configuration.
feed_import_feed_import_reader_info in ./feed_import.module
Implements hook_feed_import_reader_info().

File

feed_import_base/inc/feed_import_readers.inc, line 12
This file contains implementations of feed import readers.

View source
class SimpleXMLFIReader extends FeedImportSimpleXPathReader {

  // Namespace uris.
  protected $nsuri;

  // Namespace names.
  protected $nsname;

  // Namespace functions.
  protected $nsfunc;

  /**
   * {@inheritdoc}
   */
  public function init() {

    // Set default required options.
    $this->options += array(
      'class' => 'SimpleXMLElement',
      'options' => array(
        LIBXML_NOCDATA,
      ),
      'raw' => '',
      'stream' => NULL,
    );
    $opts = 0;
    foreach ($this->options['options'] as $opt) {
      $opts |= $opt;
    }
    $this->items = FALSE;

    // Try to fetch from URL.
    if ($this->options['url']) {
      if ($ctx = $this
        ->getStreamContext($this->options['stream'])) {
        libxml_set_streams_context($ctx);
      }
      $this->items = simplexml_load_file($this->options['url'], $this->options['class'], $opts);
    }
    elseif ($this->options['raw'] = trim($this->options['raw'])) {
      $this->items = simplexml_load_string($this->options['raw'], $this->options['class'], $opts);
    }
    else {

      // No raw or url resource provided.
      return FALSE;
    }

    // Add X inclusions, using a simple hack for SimpleXMLElement
    if ($this->items instanceof SimpleXMLElement && $opts & LIBXML_XINCLUDE) {
      dom_import_simplexml($this->items)->ownerDocument
        ->xinclude();
    }
    if (!$this->items) {
      return FALSE;
    }

    // Not needed anymore.
    unset($this->options['raw'], $opts);

    // Check for namespace settings.
    if (!empty($this->options['namespaces'])) {
      if (!is_array($this->options['namespaces'])) {
        $this->options['namespaces'] = static::cleanLines($this->options['namespaces']);
      }
      $this->nsname = $this->nsuri = array();
      foreach ($this->options['namespaces'] as $key => &$ns) {
        $ns = explode('=', $ns, 2);
        if (count($ns) != 2 || empty($ns[0]) || empty($ns[1])) {
          unset($this->options['namespaces'][$key]);
          continue;
        }
        list($this->nsname[], $this->nsuri[]) = $ns;

        // Set namespace.
        $this->items
          ->registerXPathNamespace($ns[0], $ns[1]);
      }

      // Not needed anymore.
      unset($this->options['namespaces'], $ns);
      if ($this->nsname) {
        $this->nsfunc = array(
          NULL,
          'registerXPathNamespace',
        );
      }
    }
    else {
      $this->nsname = $this->nsuri = NULL;
    }
    if (!($this->items = $this->items
      ->xpath($this->options['parent']))) {
      return FALSE;
    }
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function get() {

    // Get next item.
    $item = array_shift($this->items);

    // Register namespaces if needed.
    if ($this->nsfunc) {
      $this->nsfunc[0] =& $item;
      array_map($this->nsfunc, $this->nsname, $this->nsuri);
    }
    return $item;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FeedImportConfigurable::$options protected property
FeedImportConfigurable::cleanLines public static function Helper function to get lines of a string
FeedImportConfigurable::setOptions public function Sets options for this instance 4
FeedImportReader::$items protected property
FeedImportReader::formatPath public function Override this to preprocess your paths before they are used in map(). 2
FeedImportReader::getStreamContext public function Returns a stream context
FeedImportReader::__construct final public function Constructor of reader. Constructor is final but you'll have to implement init() to init your reader.
FeedImportReader::__destruct public function Destructor. 2
FeedImportSimpleXPathReader::map public function Returns a value mapped from obj by path. Overrides FeedImportReader::map
SimpleXMLFIReader::$nsfunc protected property
SimpleXMLFIReader::$nsname protected property
SimpleXMLFIReader::$nsuri protected property
SimpleXMLFIReader::get public function This method returns the next available item or NULL if there are no items left. Overrides FeedImportReader::get
SimpleXMLFIReader::init public function Here you'll init your reader. Overrides FeedImportReader::init