At times, for web developers, content importing appears to be the task which is far from trivial. Writing the import assignment out "from scratch" when aimed at covering each and every instance can't be practical, thus we advise sticking to the ones already in use, say, Migrate, Feeds.
Let us take a close look at Feeds module. Its architecture is made up of a range of plug-ins. The mainstream ones are:
Fetcher: it acts for supplying the data for further parsing and importation;
Parser: performs data parsing and consolidates the arrays of elements for further importing;
Processor: performs data parsing and consolidates the arrays of elements for further importing.
This article is describing how a plug-in for the parser is being written out. We are going to import an XML-file which has been received due to the use of Views Data Export. Voices may be heard stating there is no point in writing a plug-in, as out-of-the-box Feeds provide for its own plug-in that does XML parsing. That is true. In fact, so as to get it operational, one has to make some major alterations to the VDE outlet. As it is, it takes far less effort to write out a parser of your own:
name = Custom Parser description = Contains feeds plugins for XML import. core = 7.x version = 7.x-1.0 files[] = CustomParserXML.inc
hook_feeds_plugins() is being announced wherein we describe our parser plug-in:
/** * Implements hook_feeds_plugins(). */ function custom_parser_feeds_plugins() { return array( 'CustomParserXML' => array( 'name' => t('Custom XML parser'), 'description' => t('Parses XML as we want.'), 'handler' => array( 'parent' => 'FeedsParser', 'class' => 'CustomParserXML', 'file' => 'CustomParserXML.inc', ), ), ); }
/** * Parses a given file as a XML file. */ class CustomParserXML extends FeedsParser { /** * Implements FeedsParser::parse(). */ public function parse(FeedsSource $source, FeedsFetcherResult $fetcher_result) { // Loads xml file into string. @ $xml = simplexml_load_string($fetcher_result->getRaw(), NULL, LIBXML_NOERROR | LIBXML_NOWARNING | LIBXML_NOCDATA); // Got a malformed XML. if ($xml === FALSE || is_null($xml)) { return FALSE; } $items = array(); foreach ($xml->node as $node) { // Object to array conversion. $node = (array) $node; foreach ($node as $k => $v) { // We don't want to work with empty data. if (!$v) { unset($node[$k]); } else { $old_key = $k; // Converts all keys to lower case for consistency. $new_key = drupal_strtolower(str_replace('-', ' ', $old_key)); $node[$new_key] = $v; unset($node[$old_key]); } } $items[] = $node; } return new FeedsParserResult($items, $source->feed_nid); } /** * Override parent::getSourceElement() to use only lower keys. */ public function getSourceElement(FeedsSource $source, FeedsParserResult $result, $element_key) { return parent::getSourceElement($source, $result, drupal_strtolower($element_key)); } }
A parser of our own is up and running. It can be selected at the importer settings page: example.com/admin/structure/feeds/[importer_name]/parser:
A deeper digging into plug-ins nature can be done here. Attached to this article are the archive with parser modules and the importer feature with a view. Our Drupal web agency appreciates your time.