Created
April 25, 2012 19:39
-
-
Save mingan/2492731 to your computer and use it in GitHub Desktop.
Uložení $_POST do XML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// jsou odeslaná data POSTem | |
if (!empty($_POST)) { | |
$filename = dirname(__FILE__) . 'mujxmlsoubor.xml'; | |
// načte soubor, musí existovat! | |
$xml = simplexml_load_file($filename); | |
// kořenovému elementu přidá nový element record | |
$record = $xml->addChild('record'); | |
// tomu přidá potomky s hodnotami z $_POST | |
$record->addChild('element1', $_POST['element1']); | |
// můžu přidávat i hlubčí strutktury | |
$el2 = $record->addChild('element2'); | |
$el2->addChild('el2a', $_POST['element2a']); | |
$el2->addChild('el2b', $_POST['element2b']); | |
// do souboru zapše celé XML | |
file_put_contents($filename, $xml->asXML()); | |
} | |
/* | |
* Výstupem bude v tomhle případě něco jako | |
* <records> | |
* ... spousta předchozích recordů .... | |
* | |
* <record> | |
* <element1>blackacaeerefver</element1> | |
* <element2> | |
* <el2a>rferfre</el2a> | |
* <el2b>lrfjealrffre</el2b> | |
* </element2> | |
* </record> | |
* </records> | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment