Skip to main content

Use feeds module to import XML data in Drupal 7

Recently i had to setup a xml import process to import data from an external supplier on a regular basis, the filename and format was not changing and i could choose where to store the file. Following is how i set it up.

First we need to download the following modules

feeds
feeds admin ui
feeds_xpathparser
ctools
job_scheduler

enable the above modules.

We will need a sample XML so this will be the one we will use for this demonstration:

<?xml version="1.0" encoding="utf-8" ?>
<!-- Sample for demonstration of processing XML feeds with feeds module -->
<Orders>
<Order>
<CustomerID>10</CustomerID>
<Order_Number>1</Order_Number>
<Order_Total>100</Order_Total>
<Currency>£</Currency>
</Order>
<Order>
<CustomerID>11</CustomerID>
<Order_Number>2</Order_Number>
<Order_Total>40</Order_Total>
<Currency>$</Currency>
</Order>
</Orders>


Now create a content type to hold this information. I am creating a content type with name as "order_info" give some description, to make things easier i have done following settings, removed body field, removed comments, removed 'publish to frontpage' options.

Now add fields as per above sample xml files we require the following fields

CustomerID, Text, 10
Order_Number, Text, 10
Order_Total, Decimal, 10.2
Currency, Text 3

Now that this is done now create an importer using structure->feeds_imports->add importer. We will name the importer "Order info xml importer".

Base the importer on following settings:

Basic settings
===========
Attach to content type: Order_info
Periodic import: As often as possible
Import on submission: uncheck
Click "Save" important to do this at each step so you do not loose the settings.

Fetcher
======
File upload - select
Click "Save"
got to settings and select "Supply path to file or directory directly" as we want to setup a regular import from a selected location. We also assume filename will be the same and will be located in the "site/default/files/feeds/" and we will call this sample.xml

Parser
=====
select XPath XML Parser
Click "Save"

Parser->Settings
=====   =======
This setting is for specifying your path information, define what is the context and fields, context is the row of data for our example context is "Order".

Context: Order

Following will be based on your content type, this is the place where you map your fields to data in XML.

field_order_number: Order_Number
field_customerid: CustomerID
field_order_total: Order_Total
field_currency: Currency
title: Order_Number

Here we have used title as identifying and unique information, and we are using Order_Number

Processor->Node processor->Settings
========   ============   ======
Content type: Order_info
Click "Save"

Processor->Node processor->Mapping
========   ============   ======
Use source as "Xpath Expression" and target as the XML data item, when you add this will add mapping as below:

xpathparser: =>

After you have completed the assignments the mappings will look as follows:

xpathparser:0 => Order_Number
xpathparser:1 => CustomerID
xpathparser:2 => Order_Total
xpathparser:3 => Currency

xpathparser:4 => Title => Unique

As we need to identify an item in XML as unique to ensure data integrity, we can choose title of node and make sure they are marked as unique. Later we will use order_number and put this in the Title, this will also help identify the content.

After saving this, now lets import. For this we need to go to "http://yourlocal/import" and select 'Order_info' put the title as 'Order XML Import' this will create a content that allows your to import data into your Order_info content type, here you need to select location of file lets put "public://feeds/Sample.xml" and put title as "Order XML Import" and save the content.

To Import manually click 'Import' or to see how this will be imported using cron go to admin/config/system/cron and see new nodes being created from you XML.






Comments

Popular posts from this blog

Create a drupal development environment using docker.

Currently at work i am using docker to streamline and speedup of development and deployment to testing and production, I am using symfony currently and our app is dockerised, i wanted to do the same for drupal based apps.

I searched for a official drupal docker repo and i did find this. https://www.drupal.org/node/2538074

Unfortunately there was very terse explanation but pretty much worked, i found a few hiccups along the way so wanted to share what i did.

Pre-requisite:
Install docker for your environment. Lots of details are available at https://docs.docker.com/ explaing docker and installing docker for your environment.

The docker repo holds the drupal container at https://hub.docker.com/_/drupal/, following the instructions this is how to create the drupal container:

In order to use my code base we need to mount the volume with the -v directive as below, we do this in one command:

docker run -v /$HOME/work/drupal8:/var/www/html -p 8080:80 --name mydrupal -d drupal

this will create…

How do you expose your legacy database tables to views in drupal 6 Part 1

This is a 2 part post, that shows how I exposed a legacy table to views in drupal and used the incredible power of views to create many combinations static and conditional views. This was very helpful for my client.

I had a requirement that warranted exposing existing database table to views, so that views can contain data from this table.  To enable this to happen, the following steps need to be taken.
In a custom module you need to first define the views_api hook as following:
function candidate_views_api(){ $view = array(); $view = array('api' => 2, 'path' => drupal_get_path('module','candidate'). '/views'); return$view; }
candidate module was a custom module which I already had, so it made sense to add this hook here. 
The important bit here is telling the drupal sub-system where the views file will reside. 
Then create a file with your <modulename>.views.inc in my case it was candidate.views.inc, this file should have two hooks, one …