Store XML Data to PHP Variable

How to Store XML Data to PHP Variable

wprocket

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.

<?php
error_reporting(0);

// Load XML file
$filename = 'Producten.xml';

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

$doc = new DOMDocument();
    $doc->load( $filename);
    ## lets read the artikel block
    $records = $doc->getElementsByTagName( "artikel" );
    foreach( $records as $record ) {
        ## parse the artikelcode
        $artikelcode = $record->getElementsByTagName( "artikelcode" );
        $artikelcode = $artikelcode->item(0)->nodeValue;
        ## parse the _artomsch1
        $_artomsch1 = $record->getElementsByTagName( "_artomsch1" );
        $_artomsch1 = $_artomsch1->item(0)->nodeValue;
        ## parse the _artomsch2
        $_artomsch2 = $record->getElementsByTagName( "_artomsch2" );
        $_artomsch2 = $_artomsch2->item(0)->nodeValue;
        ## parse _artoms3
        $_artoms3 = $record->getElementsByTagname( "_artoms3" );
        $_artoms3 = $_artoms3->item(0)->nodeValue;
        ## parse _artoms4
        $_artoms4 = $record->getElementsByTagname( "_artoms4" );
        $_artoms4 = $_artoms4->item(0)->nodeValue;
        ## parse _artoms5
        $_artoms5 = $record->getElementsByTagname( "_artoms5" );
        $_artoms5 = $_artoms5->item(0)->nodeValue;
        ## parse _artoms6
        $_artoms6 = $record->getElementsByTagname( "_artoms6" );
        $_artoms6 = $_artoms6->item(0)->nodeValue;
        ## parse _artoms1
        $_artoms1 = $record->getElementsByTagname( "_artoms1" );
        $_artoms1 = $_artoms1->item(0)->nodeValue;
        ## parse _artoms2
        $_artoms2 = $record->getElementsByTagname( "_artoms2" );
        $_artoms2 = $_artoms2->item(0)->nodeValue;
        ## parse groeperingscode1
        $groeperingscode1 = $record->getElementsByTagname( "groeperingscode1" );
        $groeperingscode1 = $groeperingscode1->item(0)->nodeValue;

        echo "<p>Artikel Code: ".$artikelcode."<br/>Artomsch1: ".$_artomsch1."<br/>Artomsch: ".$_artomsch2."<br/>Artoms3:".$_artoms3."</p>";
    }

In the 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:

wprocket-728x98_Black

## lets read the artikel block
$records = $doc->getElementsByTagName( "artikel" )

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

foreach( $records as $record ) {
         ## parse the artikelcode
         $artikelcode = $record->getElementsByTagName( "artikelcode" );
         $artikelcode = $artikelcode->item(0)->nodeValue;
         ## parse the _artomsch1
         $_artomsch1 = $record->getElementsByTagName( "_artomsch1" );
         $_artomsch1 = $_artomsch1->item(0)->nodeValue;
         ## parse the _artomsch2
         $_artomsch2 = $record->getElementsByTagName( "_artomsch2" );
         $_artomsch2 = $_artomsch2->item(0)->nodeValue;
}

And finally, we are printing data by echoing variables.

echo "
Artikel Code: ".$artikelcode."
Artomsch1: ".$_artomsch1."
Artomsch: ".$_artomsch2."
Artoms3:".$_artoms3."
";

store XML data to PHP Variable
Echo XML Data

Conclusion

That’s it, my dear reader. More tutorials 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.

You will love the following tutorials:

GenesisPro728x90

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on pinterest
Pinterest
Share on tumblr
Tumblr
Anwer Ashif

Anwer Ashif

Founder of RainaStudio. Help businesses and individuals to create and outstand their online presence. Our success rate is measurable. Our blog served all around the world and counting.

One thought on “How to Store XML Data to PHP Variable

Leave a Reply

Your email address will not be published.

Want to speed up WordPress site?