Home Tutorials Forums Articles Blogs Movies Library Employment Press

<< Prev 5 | Next 5

Saving XML to a file when used locally

Saving xml to a file when used locally can be done in two ways, the first is this:
Cookie.setCookie(xmlobj)
and the second way is this:
yourxmlobj.toString()
FSCommand("save","filename.xml")
if you want this for use over the web then none of the above can be used, instead you will have to use a server-side script to create an xml file on your webserver, i would recommend either perl or php. The way i do it is this, i allow peopole to edit the xml structure inside of flash, then when they are done, they click save and then xml object is converted to a string:
mystring=yourxmlobj.toString()
loadVariablesNum("myscript.php?data="+mystring+"&filename=tester.xml",0)
then i send the string to a php script:
then php script then creates an xml file called tester.xml or whatever i tell it to and it then inserts the xmlstring into the newly created xml file. Voila!
Remember xml is basically just formatted text, i can post the php script here if you like its only small, or if you prefer i can post a perl script(cgi). Either way suits me, its up to you, note you will need a cgi-bin on your server to run a cgi-script and a php module installed on your server to run a php script, so find out if you have that first!
Posted by: No name | website http://
sortNodes();
// 08/11/2006
// -
// Rafael Rinaldi
// rafael.rinaldi@gmail.com
// www.rafaelrinaldi.com
var cache:Array = new Array();
var file:String = "";
var a:Number;
var b:Number;
//
var xml:XML = new XML();
xml.ignoreWhite = true;
xml.onLoad = function() {
        sortNodes();
};
xml.load(file);
//
function sortNodes() {
        var totalNodes:Number = xml.firstChild.childNodes.length;
        //
        for (a=0; a<totalNodes; a++) {
                cache.push(xml.firstChild.childNodes[a].childNodes);
        }
        //
        cache.sort();
        //
        for (b=0; b<totalNodes; b++) {
                trace(b+" - "+cache[b]);
        }
}
/*

-----
Enjoy
-----

<content>
<node>Teminy</node>
<node>Bruna</node>
<node>Amanda</node>
<node>Fernanda</node>
<node>Nathalia</node>
<node>Rafaela</node>
<node>Mayara</node>
<node>Andressa</node>
<node>Mayra</node>
<node>Viviane</node>
<node>Walesca</node>
</content>

*/

Posted by: Rafael Rinaldi | website http://www.rafaelrinaldi.com
Strip an XML node from it's tags and attributes
When you bring content into flash from an xml file you can end up with a variable containing more then just the content. If you are just placeing it into text boxes this is OK, but if you are using the content with checkers eg.
if(variable=="value"){<do stuff};
then you should use this function.
function stripNode1 (nf, nn) {
        s = String(nf);
        l = s.length;
        nl = nn.length;
        st = s.indexOf(nn)+nl;
        fi = l-(nl+3);
        n = s.slice(st, fi);
        st2 = n.indexOf(">")+1;
        n = n.slice(st2);
        return n;
}
An example of this function would be if you had a variable 'mynode' containing the following - Contents to display
You would call the function with -
mynode = stripNode1(mynode, "Text");
The value of 'mynode' after this call would be- Contents to display
Posted by: Andy Madigan | website http://www.codedanswer.com
Using XSLT to retrieve XML nodes
// //////////////////////////////////////////////////////////
// Extends XML object for ease
// of node retrieval.
// Author:(c)2002 M.Corby (matt.corby@btinternet.com)
// Status: You may exchange, modify and distribute this code
//         with permission as long as you
//         recognise the original author in your subsequent distributions.
//         I would appreciate if you sent me any updates you perform to these
//         functions :)
// Functionality: At present, you can supply an XML Object and a XSLT
//                transform statement (such as //a/b or /a/b/count(c))
//                to retrieve an array of nodes matching this criteria.
//                Use selectSingleNode to retrieve and XML Object back with a
//                single node matching XSLT criteria, and use selectNodes to
//                retrieve an array of nodes/values matching the criteria.
// Based on:      Matches Functionality provided by MSXML3 DOM
//                and DOMDocument objects
// //////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////
// XML extended Methods
//

var sumNodeNames = new Array(); // these two arrays are linked because action script in its wisdom cant do [][] as far as I can tell being a newbie at AScript.
var sumNodeValues = new Array(); // linked to above array.
//////////////////////////////////////////////////////////////
//Function: selectChildNode(XML Object, String)
//Purpose : finds within a node an object matching either a node name (eg "a"
//          or a performs a function
//          (eg "sum(b)").
//Limitations: Cannot perform functions within node criteria yet eg "a[local-//             name(first()) = 'wibble']" - in dev

function selectChildNode (XMLDoc, nodeName) {
        var nd = null;
        var formulae = "", containsFormula = false;
        
        
        // split it to see if we have some functions or criteria to meet
        
        
        if (XMLDoc.hasChildNodes) {
                nd = XMLDoc.firstChild;
                
                if(nodeName.indexOf("(", 0) > -1 && nodeName.indexOf("[", 0) == -1)
                {
                        // we have a function to perform
                        var feederVal = nd;
                        
                        funcArr = nodeName.split("(");
                        trace(funcArr);
                        
                        for(x = funcArr.length - 2; x >= 0; x--)
                        {
                                trace(typeof(feederVal));
                                if(funcArr[x] == "text")
                                {
                                        feederVal = feederVal.nodeValue;
                                        trace("converted to text: " + feederVal);
                                }
                                else if(funcArr[x] == "number")
                                {
                                        feederVal = Number(feederVal);
                                        trace("converted to number: " + feederVal);
                                }
                                else if(funcArr[x] == "sum")
                                {
                                        // store it in an array
                                        var found;
                                        for(found = 0; sumNodeNames.length; found++)
                                        {
                                                if(sumNodeNames[found] == XMLDoc.nodeName)
                                                break;
                                        }
                                        sumNodeNames[found] = XMLDoc.nodeName;
                                        sumNodeValues[found] += feederVal;
                                        
                                        trace("sumNodeNames["+found+"] = " + sumNodeNames[found]);
                                        trace("sumNodeVal["+found+"] = " + sumNodeValues[found]);
                                        
                                        feederVal = sumNodeValues[found];
                                        
                                        trace("adding to SUM value :" + sumNodeValues);
                                }
                        }
                        trace("returning:" + feederVal);
                        return feederVal;
                }
                if(nodeName.indexOf("..", 0) > -1)
                {
                        // we have a step back up the node
                        return XMLDoc;
                }
                if(nodeName.indexOf("[", 0) > -1)
                {
                        // we have an additional condition
                        // how the hell do we do this one????????
                        
                        temp = nodeName.split("[");
                        nodeName = temp[0];
                        formulae = temp[1].slice(0, temp[1].length - 1);
                        
                        containsFormula = true;
                        trace("Formula!!" + nodeName + ", " + formulae);
                }
                var found = false;
                
                while (nd.nodeName != nodeName && nd != null) {
                        trace ("->"+nd.nodeName);
                        if(containsFomula)
                        if(evaluateFunction(nd, formulae))
                        break;
                        
                        nd = nd.nextSibling;
                }
        }
        return nd;
}

function evaluateFormula(nd, formulae)
{
        // evaluates a formula from the nodeName, returns true or false.
        //evaluate things in brackets.
        // DOES NOTHING ATM
        return false;
}


var nodeArray = new Array();

function findChildNodes (XMLNode, nodeName, stopOnFirst) {
        
        var tmpnd = null, nd = null;
        
        if(stopOnFirst != true)
        stopOnFirst = false;
        
        
        
        if(XMLNode.nodeName == nodeName)
        {
                nodeArray.push(XMLNode);
                trace("pushing Node: "+ XMLNode);
                if(stopOnFirst)
                return nodeArray;
        }
        
        
        if(XMLNode.hasChildNodes)
        findChildNodes(XMLNode.firstChild, nodeName, stopOnFirst);
        
        if((nd = XMLNode.nextSibling) != null)
        findChildNodes(nd, nodeName, stopOnFirst)
        
        //trace("NODEARRAY = " + nodeArray);
        return nodeArray;
}
///////////////////////////////////////////////////////////////////////////
// function: 	selectSingleNode
// parameters:	XMLNode - an XML Object
// 			XSLTQuery - a string containing an XSLTQuery
// purpose:		To select the first node found based upon the rules of XSLT

function selectSingleNode (XMLNode, XSLTQuery) {
        // finds the first node as specified in the query, eg "//idl:nodes"
        // returns the first node.
        nodeArray = new Array();
        var node = null;
        var searchSubNodes = (XSLTQuery.substring(0, 2) == "//");
        trace("finding " + XSLTQuery);
        nodesToFind = XSLTQuery.split("/");
        nd = XMLNode;
        // nd.prototype.selectChildNode = XML.prototype.selectChildNode;
        // nd.prototype.selectSingleNode = XML.prototype.selectSingleNode;
        // fttb, we will ignore [name="wibble"] type things. ho ho ho.
        if (searchSubNodes) {
                trace ("search sub nodes!!");
                nodesToFind.shift();
                nodesToFind.shift();
                // remove first two elements
                // finds "//a" from root, then proceeds to find /b and /c if necessary
                ndArr = findChildNodes(nd, nodesToFind[0], true);
                for (y=0; y<ndArr.length; y++) {
                        for (x = 1; x < nodesToFind.length; x++) {
                                ndArr[y] = selectChildNode(ndArr[y], nodesToFind[x]);
                        }
                }
                node = ndArr[0];
        } else {
                // this will find things like "a/b/c" and "/a/b/c"
                // but not "//a/b/c"
                trace ("root:"+nd.nodeName);
                for (x=0; x<nodesToFind.length; x++) {
                        if (nd != null) {
                                nd = selectChildNode(nd, nodesToFind[0]);
                        }
                }
                node = nd;
        }
        return node;
}

///////////////////////////////////////////////////////////////////////////
// function: 	selectNodes
// parameters:	XMLNode - an XML Object
// 		XSLTQuey - a string containing an XSLTQuery
// purpose:	To select an array of nodes based upon the rules of XSLT
// 		So far we can do //a/b/c
//		//a/b/../c
//		//a/b/sum(number(text()) -- in development, see selectChildNode
//
//

function selectNodes(XMLNode, XSLTQuery) {
        // selects nodes from the list with the query, eg "//idl:nodes".
        // returns them as an array of nodes.
        nodeArray = new Array(); // re init nodeArray
        var node = null, ndArr = new Array();
        var searchSubNodes = (XSLTQuery.substring(0, 2) == "//");
        var findRoot = ((XSLTQuery.substring(0, 1) == "/") && !searchSubNodes);
        
        trace("finding " + XSLTQuery);
        nodesToFind = XSLTQuery.split("/");
        if(findRoot)
        nd = findRootNode(XMLNode);
        else
        nd = XMLNode;
        
        // fttb, we will ignore [name="wibble"] type things. ho ho ho.
        if (searchSubNodes) {
                trace ("search sub nodes!!");
                nodesToFind.shift();
                nodesToFind.shift();
                // remove first two elements
                // finds "//a" from root, then proceeds to find /b and /c if necessary
                ndArr = findChildNodes(nd, nodesToFind[0]);
                nodesToFind.shift();
                lengthOfArray = ndArr.length;
                var y = 0, w = 0; // counters
                for (y=0; y < lengthOfArray; y++) {
                        trace("y = " + y +" nodesToFind.length="+nodesToFind.length);
                        for (w = 0; w < nodesToFind.length; w++) {
                                ndArr[y] = selectChildNode(ndArr[y], nodesToFind[w]);
                                //trace("w = "+ w + ", ndArr["+y+"]="+ndArr[y]);
                        }
                }
                //trace("ndArr = " + ndArr);
                node = ndArr;
        } else {
                // this will find things like "a/b/c"
                // but not "//a/b/c"
                trace ("root:"+nd.nodeName);
                for (x=0; x<nodesToFind.length; x++) {
                        if (nd != null) {
                                nd = selectChildNode(nd, nodesToFind[0]);
                        }
                }
                node = nd;
        }
        return node;
        
}
// a little function to traverse back up to the root node of a object
function findRootNode(XMLNode)
{
        var nd = null;
        
        trace("in findRootNode");
        
        nd = XMLNode.parentNode;
        
        while(nd != null){
                XMLNode = nd;
                nd = nd.parentNode;
        }
        
        return XMLNode;
}

Posted by: Matthew Corby | website http://www.btinternet.com/~matt.corby
XML Object 2 DataSet
//================================================
// XMLObject to DataSet
//================================================
// This allows you to use a standard XML object
// to load data into a datasest. Using the standard
// allows the use of a progress bar to show the
// loading of the XML data. With the XML connector
// component it seems that there is no easy way to
// display the load status. This is designed
// for Flash MX 2004 Pro.
//------------------------------------------------
// [NOTES]
// loaderPopup is a movieclip that has a progress
// bar component in it that is tied to the progress
// of the XML obj.  btnLoadBig_jea is a button
// component that is used to trigger the XML Load.
//------------------------------------------------
loaderPopup._visible = 0; // hide progressbar loader movieclip
var mainXML:XML = new XML();// define & strict type the main xml object

// Once the XML loads, do this..
function xmlPopDsJEA () {
        _root.myDataSet.disableEvents(); // turn off events on the dataset. Processes faster.
        _root.loaderPopup._visible = 0; // hide progresbar loader MC
        _root.btnLoadBig_jea.enabled = 0; // turn off load button [optional]
        _root.btnLoadBig_jea.label = "XML Loaded"; // change the text on the load button [optional]
        numPeople = _root.mainXML.firstChild.childNodes.length - 1; // how many rows
        for (i=0; i<numPeople; i++) { // loop over every row
                myRow = _root.mainXML.firstChild.childNodes[i];
                myChildCount = _root.mainXML.firstChild.childNodes[i].childNodes.length;
                var newRow:Object = new Object(); // populate an object with the data from a row
                for (j=0; j<myChildCount;j++) {
                        var attName = _root.mainXML.firstChild.childNodes[i].childNodes[j].nodeName;
                        var attVal = _root.mainXML.firstChild.childNodes[i].childNodes[j].firstChild.nodeValue;
                        newRow[attName]=[attVal];
                }
                _root.myDataSet.addItem(newRow); // add the data from the object to a new row in the dataset
        }
        _root.myDataSet.enableEvents(); // re enable the dataset events
}

// Load xml with progress bar button handler
clippyListener = new Object();
clippyListener.click = function (evt){
        _root.loaderPopup._visible = 1; // show progressbar loader MC
        var mainXML:Object = new XML();
        _root.mainXML.ignoreWhite = "true";
        _root.mainXML.onLoad = xmlPopDsJEA; // when the xml loads do the xmlPopDsJEA function
        _root.mainXML.load("xmldata.xml"); // path to the XML file
}
this.btnLoadBig_jea.addEventListener("click", clippyListener);

Posted by: Jason3d | website http://www.jason3d.com

<< Prev 5 | Next 5

Copyright 2000-2010 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.