You are here

public static function FeedImport::processXML in Feed Import 7.2

Imports and process a feed normally

Parameters

array $feed: Feed info array

Return value

array An array of objects

File

./feed_import.inc.php, line 966
Feed import class for parsing and processing content.

Class

FeedImport
@file Feed import class for parsing and processing content.

Code

public static function processXML(array $feed) {

  // Load xml file from url.
  try {
    $xml = simplexml_load_file($feed['url'], self::$simpleXMLElement, LIBXML_NOCDATA);
  } catch (Exception $e) {
    return NULL;
  }

  // If there is no SimpleXMLElement object.
  if (!$xml instanceof self::$simpleXMLElement) {
    return NULL;
  }
  $namespaces =& $feed['xpath']['#settings'];

  // Check for namespace settings.
  if (!empty($namespaces)) {
    foreach ($namespaces as $key => &$namespace) {
      if (!$namespace) {
        unset($namespaces[$key]);
        continue;
      }
      $namespace = explode(' ', $namespace, 2);
      if (count($namespace) != 2 || empty($namespace[0]) || empty($namespace[1])) {
        unset($namespaces[$key]);
        continue;
      }

      // Set namespace.
      $xml
        ->registerXPathNamespace($namespace[0], $namespace[1]);
    }
  }
  else {
    $namespaces = array();
  }

  // Get items from root.
  $xml = $xml
    ->xpath($feed['xpath']['#root']);

  // Get total number of items.
  $count_items = count($xml);

  // Check if there are items.
  if (!$count_items) {
    return NULL;
  }

  // Check feed items.
  if (empty($namespaces)) {
    foreach ($xml as &$item) {

      // Set this item value to entity, so all entities will be in $xml at end!
      $item = self::createEntity($feed, $item);
    }
  }
  else {
    foreach ($xml as &$item) {

      // Register namespaces.
      foreach ($namespaces as &$namespace) {
        $item
          ->registerXPathNamespace($namespace[0], $namespace[1]);
      }

      // Set this item value to entity, so all entities will be in $xml at end!
      $item = self::createEntity($feed, $item);
    }
  }
  unset($feed);

  // Return created entities.
  return $xml;
}