So on to the code:
//in this example the structure that we will create is like this and stored in a file named "music.xml":
//<music>
//<newFile>
//<id>...</id>
//<title>...</title>
//<urlID>...</urlID>
//<descriptionID>...</descriptionID>
//<dateID>...</dateID>
//</newFile>
//...
//
// have in mind that the initial tag
// add the nodes in there
//we create the http request that will transfer the data to the php file upon call
<mx:HTTPService id="filePush" showBusyCursor="true" url="assets/xmlWriter.php" headers="null" requestTimeout="18" resultFormat="e4x"
contentType="application/x-www-form-urlencoded" method="GET">
<mx:request>
<id>{getID()}</id>
<title>{titlePush.text}</title>
<url>{urlString+titlePush.text}</url>
<date>{date_str}</date>
<desc>{desc.text}</desc>
</mx:request>
</mx:HTTPService>
//now we throw in some Action script
//to create a unique id for all entries
public function getID():Number
{
var start:Number=Math.round(Math.random()*today_date.getFullYear());
start=(start+(today_date.getMonth()+1)+today_date.getDay())*today_date.getSeconds();
return start;
}
//and for the Date
[Bindable]
public var today_date:Date = new Date();
public var date_str:String = (today_date.getDate()+"/"+(today_date.getMonth()+1)+"/"+today_date.getFullYear());
//==========================================
//and finaly the PHP code, pretty straightforward using SimpleXML:
<?php
$file="music.xml";
$xml=simplexml_load_file($file) or die ("Unable to load XML");
$vitals = $xml->addChild('newItemID');
$vitals->addAttribute('id', $_GET['id']);
$vitals->addChild('titleID', stripcslashes($_GET['title']));
$vitals->addChild('urlID', $_GET['urlID']);
$vitals->addChild('dateID', $_GET['date']);
$vitals->addChild('descriptionID', stripcslashes($_GET['desc']));
$doc = new DOMDocument('1.0');
$doc->preserveWhiteSpace = false;
$doc->loadXML( $xml->asXML());
$doc->formatOutput = true;
file_put_contents( $file, $doc->saveXML());
?>
for the editing part wait for my next post which won't be too late!
Enjoy!
2 comments:
Hi need some help for this, new to php and as3.
Have a XML file which contains reports... I want the user to be able to be able to add reports to this XML file without seeing the file itself.
So for the user they will see a Flex/HTML (or whatever) form with inputFields for Report Name, and report description as well as a drop down box for category.
Once a Submit button is pressed, the nodes will be added to the bottom of the XML file within the root nodes, with the values of reportname, description and category set according to the form they submitted.
Yes what I have posted here should be able to get you going, you need to setup a flex web application, create a form with the text inputs that you want. And then on submit gather those values and make a call to your .php file that will handle the writing to the xml file.
Just make sure you twist the code above to match your needs.
Post a Comment