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.
Daniel Sipos
Danny founded WEBOMELETTE in 2012 as a passion project, mostly writing about Drupal problems he faced day to day, as well as about new technologies and things that he thought other developers would find useful. Now he now manages a team of developers and designers, delivering quality products that make businesses successful.
Comments
Turn XML into an array.
I've done this recently and it worked quite well:
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.
In reply to Turn XML into an array. by thedavidmeister (not verified)
Interpret an XML
it really helped me.... thanks :)
Cool! I will definitely give
Cool! I will definitely give it a try!! But I have a couple of questions:
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
$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 see! Thanks a lot!
D
querypath all the things!
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 man
Thank you very much!!! :)
Load dynamic xml from rules
Add new comment