SimpleXml.php in Migrate Plus 8.3
File
src/Plugin/migrate_plus/data_parser/SimpleXml.php
View source
<?php
namespace Drupal\migrate_plus\Plugin\migrate_plus\data_parser;
use Drupal\migrate\MigrateException;
use Drupal\migrate_plus\DataParserPluginBase;
class SimpleXml extends DataParserPluginBase {
use XmlTrait;
protected $matches = [];
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
libxml_use_internal_errors(TRUE);
}
protected function openSourceUrl($url) {
libxml_clear_errors();
$xml_data = $this
->getDataFetcherPlugin()
->getResponseContent($url);
$xml = simplexml_load_string($xml_data);
$this
->registerNamespaces($xml);
$xpath = $this->configuration['item_selector'];
$this->matches = $xml
->xpath($xpath);
foreach (libxml_get_errors() as $error) {
$error_string = self::parseLibXmlError($error);
throw new MigrateException($error_string);
}
return TRUE;
}
protected function fetchNextRow() {
$target_element = array_shift($this->matches);
if ($target_element !== FALSE && !is_null($target_element)) {
foreach ($this
->fieldSelectors() as $field_name => $xpath) {
foreach ($target_element
->xpath($xpath) as $value) {
if ($value
->children() && !trim((string) $value)) {
$this->currentItem[$field_name] = $value;
}
else {
$this->currentItem[$field_name][] = (string) $value;
}
}
}
foreach ($this->currentItem as $field_name => $values) {
if (count($values) == 1) {
$this->currentItem[$field_name] = reset($values);
}
}
}
}
}
Classes
Name |
Description |
SimpleXml |
Obtain XML data for migration using the SimpleXML API. |