PHP 8.5.0 Beta 1 available for testing

Voting

: two plus two?
(Example: nine)

The Note You're Voting On

centy2010 at hotmail dot com
11 years ago
I filled my database from an XML file about 1260 Kb.
I made my process less than a second. Simply by using SimpleXML.

I give you a little example of the usage to fill a database.
Given the table universe.
With 4 columns Coords, Planet_Name, Player_id, Moon_Size.

Now You have a XML file named universe.xml with this kind of data.
I enclose the data within php tags but it's a file :p
<?php
$xml
= <<<EOT
<?xml version="1.0" encoding="UTF-8"?>
<universe xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" -
xsi:noNamespaceSchemaLocation="http://s127-fr.ogame.gameforge.com/api/xsd/universe.xsd" -
timestamp="1405413350" serverId="fr127">
<planet id="1" player="1" name="Arakis" coords="1:1:2">
<moon id="2" name="Mond" size="4998"/>
</planet>
<planet id="33620176" player="100000" name="GameAdmin" coords="1:1:3"/>
<planet id="33620179" player="100003" name="Heimatplanet" coords="1:1:1"/>
<planet id="33620186" player="100004" name="OGame Team" coords="6:250:1"/>
<planet id="33620242" player="100058" name="KnS" coords="9:1:6">
<moon id="33668391" name="Lune" size="8831"/>
</planet>
</universe>
EOT;
?>

Now how to bring these data in my 4 columns table :

<?php
$newfname
= $path."Universe.XML";
$mydata = new SimpleXmlIterator($newfname, 0, true);
$myquery = "INSERT INTO `".$tablename."` (`coords`,`planet_name`, `player_id`, `moon_size`) VALUES ";
for (
$mydata->rewind();$mydata->valid();$mydata->next()) {
$myquery.= " ('".$mydata->current()->attributes()['coords']
_."','".utf8_decode($mydata->current()->attributes()['name'])
_."','".utf8_decode($mydata->current()->attributes()['player'])
_."','";
if (
$mydata->haschildren()) {
$myquery.= $mydate->current()->children()->attributes()['size']."'),";
} else {
$myquery.= "'),";
}
}
$myquery = rtrim($myquery, ",");
$datatosql = mysql_query($myquery);
?>

<< Back to user notes page

To Top