WDTUTORIALS

Drupal 7 - How To Import XML Data As Nodes With The Feeds Module

Tutorial on how to use Feeds and Feeds Ex modules to import XML data.

How to install it?

drush -y en feeds feeds_ex feeds_ui

Feeds Ex module provides the XML Parser. Make sure you enable the Feeds Admin module (feeds_ui).

Import data from XML as nodes

Create a file data.xml somewhere with this content (this will be our data source):

<books>
  <book>
    <title>War and Peace</title>
    <author>Leo Tolstoi</author>
    <description>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
    </description>
  </book>
  <book>
    <title>Waking Up</title>
    <author>Sam Harris</author>
    <description>Consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
    </description>
  </book>
</books>

Create a content type called book.

Save and add fields.

Add Author text field with the default settings:

Go to Structure > Feeds Importers > Add Importer.

Add Importer and call it Book.

Basic Settings

Turn off periodic import:

Attach to content type setting allows us to import source when a specific type of node is created. With Use standalone form we will have a form to import the data.

Remember to Save each section before you move to the next.

Fetcher

Select a File upload as the fetcher:

Leave File upload settings as they are:

Parser

Select XML Xpath as the parser:

Set the parser settings.

Values in the VALUE column has to match the names of the XML tags in the source file.

Define /books/book as the context:

Processor

Leave processor as node:

Select Bundle as Book.

Select Update existing nodes:

Use administer account for the Author:

Finally map the source data to the destination fields.

SOURCE column has the sources we defined previously and TARGET column describes the target fields in the Book content type.

Make sure you define one target as unique.

Go to http://yoursite.com/import

Select Book.

Upload the XML file and hit Import:

2 nodes will be created:

Update nodes

Change the data.xml file and upload it again:

<books>
  <book>
    <title>War and Peace</title>
    <author>Leo Tolstoi</author>
    <description>Lorem CHANGED DATA ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
    </description>
  </book>
  <book>
    <title>Waking Up</title>
    <author>Sam Harris</author>
    <description>Consectetur CHANGED DATA adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
    </description>
  </book>
  <book>
    <title>Hitch-22</title>
    <author>Christopher Hitchens</author>
    <description>Consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
    </description>
  </book>
</books>

And we have 1 new node + 2 updated nodes:

Links

Samuli Natri built his first website in the late 90’s. He is very passionate about web technologies. He attended Helsinki University Of Technology (Computer Science) and Helsinki University (Social Sciences). He is the founder of WDTutorials.com.

Featured Python Tutorial For Beginners - Essentials In 1 Hour

Copyright @ 2018 WDTutorials.com. All rights reserved. Privacy policy.Terms Of Service.