Drupal CSV and XML Data Exports

ArticleMay 17, 2011

Drupal is great at allowing developers to repurpose content and display it in as many different ways as they can dream up, but what's the best way to export content from Drupal?

XML and CSV are two widely used formats that I've had to create exports for recently, and Drupal gave me the tools to do this without a problem.

The modules used in this post are as follows:

Export to XML

The problem: Export information for individual courses from a college's course catalog (built with Drupal 6), to be imported into the college's main .edu site.

The solution: Use Views Datasource to produce an XML document for each course node containing course title, number and description (including HTML markup).

The Method: Views was already installed and configured on this site, so I just added on the Views Data Export module, which provides additional display types for views.

Once the new module had been activated, I visited the views admin page and created a view (display type: data export) containing the information I was looking to export: node title, the course number field and node body. If this information was already configured as a view, a general course list maybe, I would have just needed to add a display of the type 'data export.'

Under basic settings, I selected 'XML file' as the view style...you'll want to be sure to override the default settings if this view is already being displayed elsewhere on your site. Once XML had been set as the view style, I customized the style options to get the XML output formatted to my needs.

After the basic set up, I checked the Views live preview and noticed that my nodes didn't have names. It took me a few minutes to figure out that node titles are based on the titles of the fields populating each node. Be sure to override your base settings and set node titles, if you fields don't already have titles or if you'd like to use something different in your XML document.

Next, I moved down to the export settings and set batched export to no (export data all in one segment), since I only wanted to output an individual XML file per node this wouldn't cause any time or memory limit issues.

At this point, the full XML feed was set up, but I needed to take it one step further and create an XML document for each course. To accomplish this, I added an argument (renamed to contextual filter in Drupal 7) based on the course number field; this argument could be added based on any number of fields, node IDs, etc. Once I created and saved the argument, the individual XML documents were ready to use at URLs formatted like so: www.example.com/xml/psyc101.

Export to CSV

The problem: Export a CSV of a large member directory, customized to include any number of user profile fields.

The solution: Use Views and Views Data Export to build a list of users and add the necessary data to be exported to CSV.

The Method: Since I already had Views setup to display lists of members on their respective directory pages, I just added a view with the display mode set to data export.

My final output needed to be a CSV with field names as headers, so I set the format of the data export to CSV and checked 'make first row a list of column headers' under settings.

A path needs to be set regardless of format, so I made sure to limit the accessibility of that path to users who were authorized to generate and view the CSV exports, under export settings.

Once the export settings were dialed in, I set the pager to 'display all items' because the view will only export as much data as the pager settings allow.

Finally, I added links to the directory exports paths to the shortcuts menu for authorized users to make downloading the CSV exports a little easier.