inWebdeveloper

Free and Open Source Web Resources

Recent Posts

Parsing XML With PHP

Maybe it’s a lot of tutorials that teach how to parsing XML with PHP. However, only the basics. What if a little more complicated.

This time I made a tutorial how to parsing XML with PHP, then process the data into MySQL.


First, an XML file.

<db_admin>
  <row>
    <id_admin>1</id_admin>
    <adm_nama>NAWA</adm_nama>
    <adm_userid>nawa</adm_userid>
    <adm_password>menur15b</adm_password>
  </row>
  <row>
    <id_admin>2</id_admin>
    <adm_nama>Admin</adm_nama>
    <adm_userid>admin</adm_userid>
    <adm_password>12345</adm_password>
  </row>
</db_admin>

Now, code in PHP.

<?php
require ('config.php');

$file = "mla_export_db_pengumuman.xml";

$table = explode(".",$file);
$table = explode("mla_export_",$table[0]);
$table = $table[1];

$xml = simplexml_load_file($file);
foreach($xml->row as $item){
	$val = null;
	foreach($item->children() as $child){
		$val[] = "'".$child."'";
	}
	$values = join($val, ", ");

	$sql = "INSERT IGNORE INTO $table VALUES ($values)";
	//echo $sql."<br >";
	if (!mysql_query($sql)) die('Error: ' . mysql_error());
}
?>

How, easy right. But I make this script for 2 days.

Related Reading:

Building PHP Applications with Symfony, CakePHP, and Zend Framework
Murach's PHP and MySQL (Murach: Training & Reference)
Drupal User's Guide: Building and Administering a Successful Drupal-Powered Web Site
PHP Cookbook: Solutions and Examples for PHP Programmers
PHP and MySQL Web Development (4th Edition)

Incoming search terms:

Categories: MySQL, PHP, PHP Snippets

Leave a Reply


Popular stories

Free Download: Angry Birds...

Posted on Jun - 24 - 2011

47 Comments

phpSysInfo : PHP script...

Posted on Mar - 22 - 2010

10 Comments

Free SQL Editor :...

Posted on Jul - 24 - 2010

10 Comments

Free Download Samsung Kies...

Posted on May - 7 - 2011

9 Comments

MobiOne : Free iPhone...

Posted on Apr - 18 - 2010

7 Comments

Click here for 5 Day Trial Promotion