class SimpleXml in Migrate Plus 8.5
Same name and namespace in other branches
- 8.2 src/Plugin/migrate_plus/data_parser/SimpleXml.php \Drupal\migrate_plus\Plugin\migrate_plus\data_parser\SimpleXml
- 8.3 src/Plugin/migrate_plus/data_parser/SimpleXml.php \Drupal\migrate_plus\Plugin\migrate_plus\data_parser\SimpleXml
- 8.4 src/Plugin/migrate_plus/data_parser/SimpleXml.php \Drupal\migrate_plus\Plugin\migrate_plus\data_parser\SimpleXml
Obtain XML data for migration using the SimpleXML API.
Plugin annotation
@DataParser(
id = "simple_xml",
title = @Translation("Simple XML")
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\migrate_plus\DataParserPluginBase implements DataParserPluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of SimpleXml
File
- src/
Plugin/ migrate_plus/ data_parser/ SimpleXml.php, line 16
Namespace
Drupal\migrate_plus\Plugin\migrate_plus\data_parserView source
class SimpleXml extends DataParserPluginBase {
use XmlTrait;
/**
* Array of matches from item_selector.
*
* @var array
*/
protected $matches = [];
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
// Suppress errors during parsing, so we can pick them up after.
libxml_use_internal_errors(TRUE);
}
/**
* {@inheritdoc}
*/
protected function openSourceUrl($url) {
// Clear XML error buffer. Other Drupal code that executed during the
// migration may have polluted the error buffer and could create false
// positives in our error check below. We are only concerned with errors
// that occur from attempting to load the XML string into an object here.
libxml_clear_errors();
$xml_data = $this
->getDataFetcherPlugin()
->getResponseContent($url);
$xml = simplexml_load_string(trim($xml_data));
foreach (libxml_get_errors() as $error) {
$error_string = self::parseLibXmlError($error);
throw new MigrateException($error_string);
}
$this
->registerNamespaces($xml);
$xpath = $this->configuration['item_selector'];
$this->matches = $xml
->xpath($xpath);
return TRUE;
}
/**
* {@inheritdoc}
*/
protected function fetchNextRow() {
$target_element = array_shift($this->matches);
// If we've found the desired element, populate the currentItem and
// currentId with its data.
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;
}
}
}
// Reduce single-value results to scalars.
foreach ($this->currentItem as $field_name => $values) {
if (is_array($values) && count($values) == 1) {
$this->currentItem[$field_name] = reset($values);
}
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DataParserPluginBase:: |
protected | property | Index of the currently-open url. | |
DataParserPluginBase:: |
protected | property | Value of the ID for the current item when iterating. | |
DataParserPluginBase:: |
protected | property | Current item when iterating. | |
DataParserPluginBase:: |
protected | property | The data retrieval client. | |
DataParserPluginBase:: |
protected | property | String indicating how to select an item's data from the source. | |
DataParserPluginBase:: |
protected | property | List of source urls. | |
DataParserPluginBase:: |
public | function | ||
DataParserPluginBase:: |
public static | function | ||
DataParserPluginBase:: |
public | function | ||
DataParserPluginBase:: |
protected | function | Return the selectors used to populate each configured field. | |
DataParserPluginBase:: |
public | function | Returns the initialized data fetcher plugin. | |
DataParserPluginBase:: |
public | function | ||
DataParserPluginBase:: |
public | function | Implementation of Iterator::next(). | |
DataParserPluginBase:: |
protected | function | Advances the data parser to the next source url. | 1 |
DataParserPluginBase:: |
public | function | 1 | |
DataParserPluginBase:: |
public | function | ||
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
SimpleXml:: |
protected | property | Array of matches from item_selector. | |
SimpleXml:: |
protected | function |
Retrieves the next row of data. populating currentItem. Overrides DataParserPluginBase:: |
|
SimpleXml:: |
protected | function |
Opens the specified URL. Overrides DataParserPluginBase:: |
|
SimpleXml:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides DataParserPluginBase:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
XmlTrait:: |
public static | function | Parses a LibXMLError to a error message string. | |
XmlTrait:: |
protected | function | Registers the iterator's namespaces to a SimpleXMLElement. |