Need some help with your project? Contact me

How to parse an XML file

This tutorial will explore a way to parse an XML file for use in .php.

Say you have an XML file you want to parse in order to add its contents to a database. Or you have an XML feed you need to parse regularly for some purpose. You can use the simplexml_load_file() .php function to transform the content of the XML file into a ready to use .php object.

I did this when performing some tests with a Drupal module, since I couldn’t find a Drupal alternative to interpret XML files (maybe there is one, I just couldn’t find it). So a solution I did find was regular .php (you have to replace "module_name" with the name of your module and "file.xml" with the name of the XML file):

if (file_exists(drupal_get_path('module', 'module_name') . '/file.xml')) {
  $xml = simplexml_load_file(drupal_get_path('module', 'module_name') . '/file.xml'); 
}

What this bit of code does is first checks if the XML file actually exists. If it does, it loads it from the path and interprets it, creating an object that can be used in .php.

I used here the function drupal_get_path() which directly finds the path to the respective module. If you would like to use this bit of code outside Drupal, you should replace the entire argument of the simplexml_load_file() function with the path to the XML file.

And this is pretty much it. This piece of code should work, but I am also curious to know if there is some ‘Drupal’ way of doing something like this. For example, I would very much like to see if there is a way to interpret an XML file into an array. If you know of anything, please do share.

Hope this helps.

Comments

I've done this recently and it worked quite well:

// Turn our XML string into a nested, associative array.
$data = new SimpleXMLElement($request->data);
$data = drupal_json_decode(drupal_json_encode($data));

Just remember that you want to use empty() instead of != FALSE to check for "emptiness" as things that are empty strings in your XML turn into empty *arrays* after conversion.

it really helped me.... thanks :)

Cool! I will definitely give it a try!! But I have a couple of questions:

$data = drupal_json_decode(drupal_json_encode($data));

Isn't this redundant? Why encode it so you then decode it?

And the XML feed is in your $request->data no?

Thanks!

$request in that snippet comes from drupal_http_request() so yeah, $request->data is the XML string.

The encoding then decoding is just tricking PHP into turning it into an array for you by encoding it as a JSON string first. Probably not the highest performance way to do it, but it is likely the easiest to code.

I see! Thanks a lot!

D

I like QueryPath - http://drupal.org/project/querypath

I use it for a variety of purposes including XML and find that makes my life pretty easy.

Thank you very much!!! :)

<?php
// this code is to parse xml file stored inside a node->field and called from rules
// funtion from my rules action
function procesarfactura_rules_action_info() {
  $actions = array(
    'procesar_factura_xml' => array(
        'label' => t('Procesar factura'),
        'group' => t('factura'),
        'parameter' => array(
            'node' => array(
                'type' => 'node',
                'label' => t('Nodo a procesar'),
                ),
            'file' => array(
                    'type' => 'file',
                    'label' => t('Archivo a procesar'),
                    ),
        ),
      ),
    );
      return $actions;
}

// code to load the file and parse xml content
function procesar_factura_xml($file);
$uri = $file->field_factura_xml['und'][0]['uri'];
$files = file_load_multiple(array(), array('uri' => $uri));
$file = reset($files); // If empty, $file will be false, otherwise will contain the required file
$xml = simplexml_load_file($uri);

// now you have a xml string to parse from variable $xml.
// here goes code to parse a xml string.

?>

Add new comment

You can post comments in Markdown and basic HTML tags.
For code blocks, wrap your code within '~~~'. For example:
~~~
$var = 'my variable';
~~~