How to parser xml in php and store in database

How to parser xml in php and store in database
Now fisrt we must to creat php file we called for example: get-calling-card-direct.php with PHP editor is Notepad++.

 

code:
<?php $doc = new DOMDocument(); ?>
What is XML DOM
In this tutorial we will use XML DOM
The XML DOM defines a standard way for accessing and manipulating XML documents.

The DOM presents an XML document as a tree-structure.

Knowing the XML DOM is a must for anyone working with XML.

Affter that we will Load XML file
DOMDocument::load ( string $filename [, int $options = 0 ] )
Loads an XML document from a file. $filename is the path to the XML document.
Code will like that:
<?php
$doc = new DOMDocument();
$filename = 'http://www.telecalls.co.uk/cron/get-calling-card-direct.xml';
$doc->load( $filename );//Loads an XML document from a file.
echo $doc->saveXML();//Dumps the internal XML tree back into a string
?>

Now we can see result in your browser with internal XML tree.

Depending on your XML we get Elements and then store in databse.
In this case we will get ‘web_rate_elements’.
DOMDocument::getElementsByTagName — Searches for all elements with given local tag name
DOMDocument::getElementsByTagName ( string $name )
$name The local name (without namespace) of the tag to match on. The special value * matches all tags.
The Code will look like this:
<?php
$doc = new DOMDocument();
$filename = 'http://www.telecalls.co.uk/cron/get-calling-card-direct.xml';
$doc->load( $filename );//Loads an XML document from a file.
echo $doc->saveXML();//Dumps the internal XML tree back into a string
$web_rate_elements = $doc->getElementsByTagName( "web_rate_element" );
?>

Now we will Prepare for databse
Open a connection to a MySQL Server with mysql_connect PHP function
mysql_select_db — Select a MySQL database

The Code will look like this:

<?php
$doc = new DOMDocument();
$filename = 'http://www.telecalls.co.uk/cron/get-calling-card-direct.xml';
$doc->load( $filename );//Loads an XML document from a file.
echo $doc->saveXML();//Dumps the internal XML tree back into a string
$web_rate_elements = $doc->getElementsByTagName( "web_rate_element" );
if(!$web_rate_elements){exit;}//check for elements
//Connect databse
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
// make database_name the current db
$db_selected = mysql_select_db('databse_name', $link);
if (!$db_selected) {
die ('Can't use database_name : ' . mysql_error());
}
mysql_close($link);
?>

Store data to databse

<?php
$doc = new DOMDocument();
$filename = 'http://www.telecalls.co.uk/cron/get-calling-card-direct.xml';
$doc->load( $filename );//Loads an XML document from a file.
echo $doc->saveXML();//Dumps the internal XML tree back into a string
$web_rate_elements = $doc->getElementsByTagName( "web_rate_element" );
if(!$web_rate_elements){exit;}//check for elements
//Connect databse
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
// make database_name the current db
$db_selected = mysql_select_db('databse_name', $link);
if (!$db_selected) {
die ('Can't use database_name : ' . mysql_error());
}

foreach( $web_rate_elements as $web_rate_element )
{
$destinations = $web_rate_element->getElementsByTagName( "destination" );
$access_numbers = $web_rate_element->getElementsByTagName( "access_number" );
$rates = $web_rate_element->getElementsByTagName( "rate" );
$sms_rates = $web_rate_element->getElementsByTagName( "sms_rate" );
$bt_rates = $web_rate_element->getElementsByTagName( "bt_rate" );

//Get Values
$destination = trim($destinations->item(0)->nodeValue);
$access_number = $access_numbers->item(0)->nodeValue;
$rate = $rates->item(0)->nodeValue;
$sms_rate = $sms_rates->item(0)->nodeValue;
$bt_rate = $bt_rates->item(0)->nodeValue;
}
mysql_close($link);
?>

 

Insert Data Into a Database Table

The INSERT INTO statement is used to add new records to a database table.
INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...)
The Code will look like this:
<?php
$doc = new DOMDocument();
$filename = 'http://www.telecalls.co.uk/cron/get-calling-card-direct.xml';
$doc->load( $filename );//Loads an XML document from a file.
echo $doc->saveXML();//Dumps the internal XML tree back into a string
$web_rate_elements = $doc->getElementsByTagName( "web_rate_element" );
if(!$web_rate_elements){exit;}//check for elements
//Connect databse
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
// make database_name the current db
$db_selected = mysql_select_db('databse_name', $link);
if (!$db_selected) {
die ('Can't use database_name : ' . mysql_error());
}

foreach( $web_rate_elements as $web_rate_element )
{
$destinations = $web_rate_element->getElementsByTagName( "destination" );
$access_numbers = $web_rate_element->getElementsByTagName( "access_number" );
$rates = $web_rate_element->getElementsByTagName( "rate" );
$sms_rates = $web_rate_element->getElementsByTagName( "sms_rate" );
$bt_rates = $web_rate_element->getElementsByTagName( "bt_rate" );

//Get Values
$destination = trim($destinations->item(0)->nodeValue);
$access_number = $access_numbers->item(0)->nodeValue;
$rate = $rates->item(0)->nodeValue;
$sms_rate = $sms_rates->item(0)->nodeValue;
$bt_rate = $bt_rates->item(0)->nodeValue;

$sql = "INSERT INTO databse_name (destination, access_number, rate, sms_rate, bt_rate, mobile) VALUES ('$destination' ,'$access_number' ,'$rate' , '$sms_rate' ,'$bt_rate', '$mobile')";

@mysql_query($sql);

}
mysql_close($link);
?>

Here is results: http://www.telecalls.co.uk/
You can download source code here http://www.telecalls.co.uk/cron/get-calling-card-direct.txt (Changes txt to php affter download)
Goodluck

Sory for my bad English, if this good for you or have any problems, please comments

Be Sociable, Share!

Leave a comment