note 115383 added to class.simplexmliterator
| From: | centy2010 at hotmail dot com | Date: | Wed, 16 Jul 2014 03:22:12 +0000 |
| Subject: | note 115383 added to class.simplexmliterator | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-199938@lists.php.net to get a copy of this message | ||
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);
?>
----
Server IP: 72.52.91.14
Probable Submitter: 95.182.172.60
----
Manual Page -- http://php.net/manual/en/class.simplexmliterator.php
Edit -- https://master.php.net/note/edit/115383
Del: integrated -- https://master.php.net/note/delete/115383/integrated
Del: useless -- https://master.php.net/note/delete/115383/useless
Del: bad code -- https://master.php.net/note/delete/115383/bad+code
Del: spam -- https://master.php.net/note/delete/115383/spam
Del: non-english -- https://master.php.net/note/delete/115383/non-english
Del: in docs -- https://master.php.net/note/delete/115383/in+docs
Del: other reasons-- https://master.php.net/note/delete/115383
Reject -- https://master.php.net/note/reject/115383
Search -- https://master.php.net/manage/user-notes.php