Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7080539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:48:28+00:00 2026-05-28T06:48:28+00:00

I have xml files been imported onto an FTP server. This is stored in

  • 0

I have xml files been imported onto an FTP server. This is stored in location ‘/public_html/ctrackxml/’ with a random file name and in the following format:

<message type="POSITIONDATA">
  <messageid>-1</messageid>
  <mobile>SNK261GP</mobile>
  <time>2012/01/20 08:34:45 AM</time>
  <latitude>-29.8477</latitude>
  <longitude>30.9554</longitude>
  <status>Driving</status>
  <speed>82</speed>
  <address> near Outer Ring Road (N2); Umkumbaan; in Durban</address>
  <direction>
  </direction>
  <runningodo>1587000</runningodo>
</message>

I need to loop through all the files in the folder and import each file into the MySQL database table xmldata which has the following structure:

MySQL table

I need each tag in the xml file to be imported into a seperate field in the table. so each xml file represent one table entry.

From the research I have done it looks like I need to use the ‘LOAD XML LOCAL INFILE’ mysql syntax however I cant seem to get this to work correctly directly within mysql.

If you could point me in the right direction I would greatly appreciate it.

Update

below is the code that I hav emanaged to scrape together with the assistance of another website.
http://www.phpfreaks.com/forums/index.php?topic=244744.0

I tested the script from phpfreaks and it works 100% however the xml structure is quite different. I have tried to modify the code to suite my xml file but am having some issues to get this working.

my code is as follows but currently fails ont he foreach statement:

<?php

echo "starting <br><br>";
//mysql connection
$con2 = mysql_connect("localhost","dbuser","dbpassword");
if (!$con2)  {  die('Could not connect: ' . mysql_error());  }
$selectdb = mysql_select_db("dbname", $con2);
if (!$selectdb)  { die('Database not used: ; ' . mysql_error());  }

echo "connected to DB<br/><br/>";

//simplexml load xml file   
$library =  simplexml_load_file('http://www.website/ctrackxml/CTO_20120119140006_0000.xml');

echo "xml loaded<br/><br/>";

//loop through parsed xmlfeed and print output      

foreach ($message->message as $message) {                  
printf("messageid: %s\n", $messageid->messageid);                  
printf("mobile: %s\n", $mobile->mobile);
printf("time: %s\n", $time->time);
printf("latitude: %s\n", $latitude->latitude);
printf("longitude: %s\n", $longitude->longitude);
printf("status: %s\n", $status->status);
printf("speed: %s\n", $speed->speed);
printf("address: %s\n", $address->address);
printf("direction: %s\n", $direction->direction);
printf("runningodo: %s\n", $runningodo->runningodo);

echo "xml parsed<br/><br/>";

//insert into databse                     
mysql_query("INSERT INTO xml (messageid, mobile, time,latitude,longitude,status,speed,address,direction,odometer)
VALUES (\"$messageid->messageid\", \"$mobile->mobile\", \"$time->time\", \"$latitude->latitude\", \"$longitude->longitude\", \"$status->status\", \"$speed->speed\", \"$address->address\", \"$direction->direction\", \"$runningodo->runningodo\")")
or die(mysql_error());

echo "inserted into mysql<br/><br/>";

//show updated records            
printf ("Records inserted: %d\n", mysql_affected_rows());  
}


//close connection 
mysql_close($con2);

?>

Thanks as always to everyone for the help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:48:28+00:00Added an answer on May 28, 2026 at 6:48 am
    <?php
    
    ini_set('display_errors','On');
    
    echo "starting";
    
    //mysql connection
    $con2 = mysql_connect("localhost","dbuser","dbpass");
    if (!$con2)  {  
        die('Could not connect: ' . mysql_error());  
    }
    
    $selectdb = mysql_select_db("dbname", $con2);
    if (!$selectdb)  { 
        die('Database not used: ; ' . mysql_error());  
    }
    
    echo "connected to DB<br /><br />";
    
    // Read filenames in current directory looking for XML files
    $file_arr = array();
    if ($handle = opendir('.')) {
        while (false !== ($file = readdir($handle))) {
            if (($file != ".") && ($file != "..")) {
                if(substr($file, -4) == ".xml")
                {
                    array_push($file_arr, $file);
                }
            }
        }
        closedir($handle);
    }
    
    // Loop through each XML file in the current directory
    foreach($file_arr as $filename)
    {
        //simplexml load xml file   
        $mess = simplexml_load_file($filename);
        echo "xml loaded<br /><br />";
    
        $messageid = mysql_real_escape_string($mess->messageid);
        $mobile = mysql_real_escape_string($mess->mobile);
        $time = mysql_real_escape_string($mess->time);
        $latitude = mysql_real_escape_string($mess->latitude);
        $longitude = mysql_real_escape_string($mess->longitude);
        $status = mysql_real_escape_string($mess->status);
        $speed = mysql_real_escape_string($mess->speed);
        $address = mysql_real_escape_string($mess->address);
        $direction = mysql_real_escape_string($mess->direction);
        $runningodo = mysql_real_escape_string($mess->runningodo);
    
        echo "xml parsed<br /><br />";
    
        //insert into databse                     
        mysql_query("INSERT INTO xml (messageid, mobile, time, latitude, longitude, status, speed, address, direction, odometer)
        VALUES ('$messageid', '$mobile', '$time', '$latitude', '$longitude', '$status', '$speed', '$address', '$direction', '$runningodo')")
        or die(mysql_error());
    
        echo "inserted into mysql<br /><br />";
    
        //show updated records            
        printf ("Records inserted: %d\n", mysql_affected_rows());  
    }
    //close connection 
    mysql_close($con2);
    
    ?>
    

    The above code should achieve what you are looking for based on the spec. As stated there is one XML record per one XML file.

    This code is designed to run in the same directory as the XML files, you can easily change this by editing opendir command in the code. It will read all the XML files in the current working directory and place the data into the Database.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have always been using jaxb for parsing XML files into java object. This
I want to commit a large amount of XML files which have been modified.
I have been thinking about the optimal way to create an XML file using
I have several xml files, the names of which are stored in another xml
I have been using fop 0.95 to generate pdf files from xml data. I
I have been using a product which displays parsed XML files as HTML. It
I've been given some XML files that don't quite have a proper schema (I
I have been creating multiple background threads to parse xml files and recreate new
I've been looking at some of the xml files excel generates and have found
I have a large file (English Wikipedia articles only database as XML files). I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.