Learn how to import data from an XML file with Drupal 8 in 5 easy steps. Our Drupal Support team is here to help you with your questions and concerns.
Import data from an XML file with Drupal 8 in 5 Steps
Migrating content into Drupal 8 or 9 requires navigating a delicate balance, especially when handling structured data such as XML or CSV files.
Fortunately, the Drupal ecosystem offers us several tools in the form of Migrate modules to make this more manageable.
Today, we will walk through a complete example of migrating XML data into Drupal using a custom migration module.
An Overview:
Step 1: Set Up the Migration Module
After installing the required Migrate modules, the first step is to create a custom module where we have to write our migration logic.
We can create the module structure and `info.yml` file as seen here:
name: test migration
type: module
description: Migrating data from the XML files.
core_version_requirement: ^8 || ^9
package: Migration
dependencies:
- drupal:migrate_source_csv
- drupal:migrate_plus
- drupal:migrate_tools
config_devel:
install:
- migrate_plus.migration_group.test_migration
- migrate_plus.migration.test_migration_sessions
- migrate_plus.migration.test_migration_speaker
- migrate_plus.migration.test_migration_user
Copy Code
Enable the module after creating it.
Step 2: Create a Migration Group
A migration group organizes related migration definitions. Then, create the migration file at this path:
test_migration/config/install/migrate_plus.migration_group.test_migration.yml
id: test_migration
label: test Migration
description: Migrating XML data
Copy Code
Remember to make sure this group is referenced in the module’s `.info.yml` file as shown earlier.
Step 3: Define a Migration Script
Now, create a new file inside the `config/install` directory to define how content should be migrated. The file name should be migrate_plus.migration.test_migration_speaker.yml.
id: test_migration_speaker
label: 'Migrate Speaker data from the XML file'
migration_group: test_migration
source:
plugin: url
data_fetcher_plugin: file
data_parser_plugin: xml
urls: private://xml/speakers.xml
item_selector: /conference/speakers/speaker
fields:
- name: speaker_id
label: 'Speaker ID'
selector: id
- name: full_name
label: 'Full name'
selector: name/fullname
- name: bio
label: 'Biography'
selector: biography
- name: affiliation
label: 'Affiliation'
selector: affiliation
- name: country
label: 'Country'
selector: country
- name: photo
label: 'Profile photo'
selector: photo
ids:
speaker_id:
type: string
process:
title:
- plugin: skip_on_empty
method: process
source: full_name
field_biography/value: bio
field_biography/format:
plugin: default_value
default_value: "full_html"
field_affiliation: affiliation
field_country: country
field_profile_photo:
plugin: file_copy
source: photo
destination: 'public://speaker_photos'
destination:
plugin: 'entity:node'
default_bundle: speaker
migration_dependencies:
required: { }
dependencies: { }
Copy Code
Understanding the Migration File
Here’s a quick breakdown of some key elements:
- id: Unique identifier for each migration YAML.
- source: Plugin and data fetcher configurations.
- item_selector: Specifies where in the XML file the data set begins.
- fields: Maps XML tags to field variables.
- ids: Unique identifiers for content.
- process: Maps source fields to Drupal fields. In this case, we also use the `file_copy` plugin to migrate speaker photos.
- destination: Target entity and bundle.
We can also write custom plugins.
Step 4: Running the Migration
Go to Structure > Migrations in the Drupal admin interface. We will see the list of migration groups.
- Click on List migration to view individual migrations.
- Then, hit Execute to perform migration actions:
- Import: Run the migration and import data.
- Rollback: Undo the migration.
- Stop: Cancel a running migration.
- Reset: Set migration status back to idle.
Step 5: Use Drush for Terminal-Based Migration
Here are some useful Drush commands for working with migrations:
drush ms # Show migration status
drush mim migration_id # Run migration import
drush mr migration_id # Rollback migration
drush mrs migration_id # Reset migration status
Copy Code
[Need assistance with a different issue? Our team is available 24/7.]
Conclusion
Migrating data to Drupal 8 or 9 can be made easy with the right tools and a well-structured approach. With Migrate modules and custom YAML scripts, we can handle complex data formats, such as XML, and map them to content types effortlessly.
In brief, our Support Experts demonstrated how to import speaker data from an XML file into Drupal 8 or 9 using a custom migration YAML file.
0 Comments