A Flash Developer Resource Site

Results 1 to 16 of 16

Thread: Changing XML

  1. #1
    Member
    Join Date
    Feb 2007
    Posts
    46

    Changing XML

    Ive made a xml file with a Message attribute and im using flash to do all this.

    Its all sorted and i gave all the messages their own variable for example...

    _global.Message[0] = first_Message

    so i can show "first_Message" using dynamic text but is it possible to CHANGE XML data? For example if "first_Message" was:

    Code:
    Hello this is a message
    could i then click a button to go to a page where i change "first_Message" to something like:

    Code:
    Hello this is a message ive just changed

    Basically changing the XML file without going into the XML file... a bit like a little admin control panel so you can change text around the site... on the site.

  2. #2
    up to my .as in code Chris_Seahorn's Avatar
    Join Date
    Dec 2004
    Posts
    4,389
    Once you have the XML object in Flash, you can alter it but the changes will only be reflected while the movie is running. To change the data AND store the changes you will need to send the altered XML object back to the server to have an intermediary (php, etc) write the changes.

    Flash cannot write any data without help from a server side (or use of a wrapper like SWF Studio if desktop flash).

    As for examples I would search this XML forum here for "write xml" or Google "writing XML with Flash" or similar.

  3. #3
    Member
    Join Date
    Feb 2007
    Posts
    46
    Ok cool thanks.

  4. #4
    Member
    Join Date
    Feb 2007
    Posts
    46
    Ive looked about but i can't seem to find much on actually editing the file.

    All i really wanna do is just change the text inbetween a tag which is already assigned to a dynamic field on flash by using an input field on the swf and pressing save. Im guessing when the save button is pressed the info in the input section is taken and put into the xml file as a replacement as what it was before.

    I don't really know much about php so if you could help me out that would be great.

  5. #5
    up to my .as in code Chris_Seahorn's Avatar
    Join Date
    Dec 2004
    Posts
    4,389
    Tell you what...attach your current fla and your current xml (as a zip) and I'll see what I can do to adapt it with a basic PHP backend that rewrites the xml. I need both those things though so I can see your current xml structure as well as the flash frontend.

    If it's personal code you don't want out there flapping in a forum PM me a link where I can grab it.

  6. #6
    Member
    Join Date
    Feb 2007
    Posts
    46
    Ok here it is...

    thanks btw
    Attached Files Attached Files

  7. #7

  8. #8
    up to my .as in code Chris_Seahorn's Avatar
    Join Date
    Dec 2004
    Posts
    4,389
    Ok man...this one is pretty straight forward. It's altered to use two functions....one to read the XML...one to write it. There are tons of ways to handle this, especially if writing multiple nodes but as it stands since we are writing one I chose to handle it on the frontend with a loadVars and the actual XML creation on the backend in the PHP file. Some examples I've done in here write the XML in the frontend (bouncing through a "for" loop or reassembling from altered arrays) and then send the whole xml packet out but you can advance this as you see fit.

    The first function reads your xml as usual. The title field and message field are now input textfields...the date field is not. When you edit the title and message and save...the loadVars sends it to the backend....gets the current date with PHP...strafes it to get the structure you are used to (xx/xx/xx) and then writes the xml using your title POST, message POST and strafed date. When it's done writing it sends a variable back into flash to tell the loadVars it's all good and then the loadVars re-reads the xml. It's fast as hell so you won't even see the re-read...to you it looks just like what you just edited....refresh the movie or direct hit the xml and you will see it did...in fact..write the new xml

    The XML file needs write permission on your server (ask your host how to chmod files if need be)!

    Security is on you and other than standard stripslashes on the backend it's up to you to protect the POST variables further. Also, my running example will refuse to post if the title or message field are empty but you would most likely create a movieclip to warn the user if they submit empty.
    Attached Files Attached Files

  9. #9
    Member
    Join Date
    Feb 2007
    Posts
    46
    That is brilliant! Thanks a lot, ive got it on my server and its working fine so you can take it off your server.

    Thanks again! I wasn't planning on being very active on here but i sure will be now, brilliant help!

  10. #10

  11. #11
    Member
    Join Date
    Feb 2007
    Posts
    46
    One question, i think the time is in American time or something else because i just edited some text for testing and it's changed to 18/02/07 (or 02/18/07, i changed the month and day around).

    Im in England so is their a way to change the date so it's english time?

  12. #12
    up to my .as in code Chris_Seahorn's Avatar
    Join Date
    Dec 2004
    Posts
    4,389
    Use an offset:

    <?php


    $mybody = stripslashes($_POST["message"]);
    $mytitle = stripslashes($_POST["title"]);

    //hour difference between your local time and server time
    $hourdiff = "+1";

    $adjust = ($hourdiff * 60 * 60);
    $today = date("m-j-y",time() + $adjust);

    if(!$mytitle || !$mybody){
    print "&returnMe=Server Error!";
    exit;
    }else{
    $xml="<?xml version=\"1.0\" encoding=\"utf-8\"?>\n\n";
    $xml.="<site>\n";
    $xml.="<aboutMe Title=\"".$mytitle."\" date=\"".$today."\" Message=\"".$mybody."\" >\n";
    $xml.="</site>";

    $file= fopen("test.xml", "w");

    fwrite($file, "$xml");
    print "&returnMe=1";

    }
    ?>

  13. #13
    up to my .as in code Chris_Seahorn's Avatar
    Join Date
    Dec 2004
    Posts
    4,389
    Here that is in action....the top date is my current local....
    the bottom date is me using an offset of "-18" (minus 18 hours)

    http://flex.hobby-site.com/today.php

  14. #14
    Member
    Join Date
    Feb 2007
    Posts
    46
    Ok thanks a lot!

    Im testing it on mine now.

  15. #15
    Member
    Join Date
    Feb 2007
    Posts
    46
    Working brilliant, thanks a lot!

  16. #16
    Member
    Join Date
    Sep 2000
    Posts
    70
    Hi Chris

    Ive just now playing with your xml/php and flash for my Photo album running
    with flash and xml

    Is there a simple way to get all nodes listed in / loaded in the flash


    like this:

    <?xml version="1.0" encoding="utf-8" ?>
    - <photos>
    <photo jpgfile="three.jpg" desc="NEW TIMELINE TO AS CONVERTER" />
    <photo jpgfile="test.jpg" desc="HAPPY NEWYEAR" />
    <photo jpgfile="chris.jpg" desc="Sailing" />
    <photo jpgfile="test.jpg" desc="HAPPY NEWYEAR" />
    </photos>



    this is my working as.code

    XML.prototype.ignoreWhite = true;
    function readXML() {
    var myXML:XML = new XML();

    myXML.load("test.xml");
    myXML.onLoad = function(success){

    if (success){

    testArray = new Array();
    testArray = this.firstChild.childNodes;

    _global.jpgfile= new Array();
    _global.desc = new Array();

    var i = 0;

    while ( i < testArray.length) {

    _global.jpgfile.push(this.firstChild.childNodes[i].attributes.jpgfile);
    _global.desc.push(this.firstChild.childNodes[i].attributes.desc);

    i = i + 1;

    }

    Photo = _global.jpgfile[0];
    Description = _global.desc[0];

    }
    }
    }
    function buildXML() {
    var result_lv= new LoadVars();
    result_lv.onLoad = function(success) {
    if (success) {
    readXML();
    } else {

    }
    };
    var send_lv = new LoadVars();
    send_lv.title = Photo;
    send_lv.message = Description;
    send_lv.sendAndLoad("writeit.php", result_lv, "POST");
    }

    change_but.onPress=function(){
    if(Photo=="" || Description==""){
    //do squat
    }else{
    buildXML();

    }
    }
    readXML();

    Regards Flemming

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center