How to Store XML Data to PHP Variable « RainaStudio

How to Store XML Data to PHP Variable

Another good day! And welcome to my one of backend code tutorial article. Here on this article I am going to show you how to store XML data to PHP variable. This is one recent project where there was a requirement to process the XML file and store data in PHP variable so that the data can be stored in database.

Step 1:  Store XML Data to PHP Variable

The way is very simple and straight forward. There is a file in our directory called Producten.xml this file contains product data. Each product or parent element called artikel has around 45 sub elements or attributes.

So, now, we are going to create a file called import2.php and start with PHP start tag.

As our XML file in save directory so we load it simply. Now move it to 2nd part.

In last part, we are getting parent element by its tag name and then, we are parsing each sub element under parent element and store them to a variable.

For example, we are getting parent by following:

And then as there are many artikel tag, we use foreach. Let’s see next block:

And finally we are printing data by echoing variables.

store XML data to PHP Variable
Echo XML Data


That’s it my dear reader. More tutorial like this on way to process at RainaStudio for. As soon as we have free time we will showcase for you.

Let me know how is this tutorial helps you or if there any question you would like to ask.

Anwer Ashif

I like to quote code snippet, fix things like error 503 service unavailable. Cycling, traveling and plantation are my hobbies.