<?xml version="1.0" ?>
<FAMILY surname= "Smith" location= "New York">
<SON name="Dave" bday= "08/10" />
<SON name="Bob" bday= "05/06" />
<DAUGHTER married_name="Perone">
<GRANDCHILD age="5" />
<GRANDCHILD age="6" />
</DAUGHTER>
</FAMILY>
Downloading XML from a server is pretty much like any other thing.
loader = new URLLoader(new URLRequest("http://www.example.com/family.xml"));
loader.addEventListener(Event.COMPLETE, xmlDownloaded);
Once we've downloaded the XML though, Actionscript 3 has a bunch of convenient things built right into the language to access the XML elements in various ways.
function xmlDownloaded(event:Event):void {
var xmlData:XML = new XML(event.target.data);
var surname:String= xmlData.@surname;
var location:String= xmlData.@location;
// get the first granddaughter's age of the first daughter
var grandchild_age:Number= Number(xmlData.DAUGHTER.GRANDCHILD.@age);
// search through all elements inside FAMILY
for each (var object:XML in levelData.*) {
if (object.name() == "SON") {
var bday:String= object.@bday;
} else
if (object.name() == "GRANDCHILD") {
var age:Number= object.@age; // will automatically convert to Number
} else
if (object.name() == "DAUGHTER") {
var num_grandchildren:uint= object.*.length();
}
}
// search through all child nodes of DAUGHTER
for each (var:grandchildren:XML in levelData.DAUGHTER.*) {
var ageStr:String= grandchildren.@age;
}
}
Accessing properties of an element is as easy as using the '.@' operator and accessing children is just simply typing .whateverchildname (you can also get any XML elements's name and attributes with the .name() and .attribute() functions). To access all elements at some point in the tree you simply use the '*' wildcard operator.One important thing to notice above is the xmlData.DAUGHTER.GRANDCHILD.@age. There are multiple GRANDCHILD's and potentially multiple DAUGHTERS (if we so choose to add more). What does it mean to get the age attribute then?
What's going on here is that you can simplify indexing into the first child element of a node by omiting the [0]. This was done to simplify cases where a node only has 1 child.
One final important function is the .parent() which gets the parent node of the element.
Let's take a look at how to change the XML data and upload it back to the server.
function uploadXML():void {
var family:XMLList= new XMLList();
// add a son named Will to the end of FAMILY
family += <SON name="Will" bday="01/15" />
family += <DAUGHTER married_name="Potter" />
var grandchildren:XMLList = new XMLList();
grandchildren += <GRANDCHILD age="1" />
// associate the grandchildren with the last child
family[family.length()-1].setChildren(grandchildren);
// get children by variable if you don't know the element name upfront and set an attribute
var childvar:String= 'SON';
family.elements(childvar).@surname= 'Perone';
// upload the XML to a php script
var loader:UrlLoader = new URLLoader();
var request:URLRequest= new URLRequest("http://www.example.com/uploadscript.php");
request.contentType = "text/xml";
request.data = family.toXMLString(); // convert to a string
request.method = URLRequestMethod.POST;
loader.load(request);
}
Pretty easy huh? All you have to do is a += to add new XML nodes. You can also deliberately set the children to a collection of nodes (XMLList) by calling setChildren and there is even insertChildAfter and insertChildBefore (not demonstrated here).The easiest way I've found to upload the XML data to the server is to set the content type as text/xml and just upload it as the data rather than setting up a URLVariable to be equal to the text.
<?php
$fh = fopen("family.xml", 'w') or die("can't open file");
fwrite($fh, $HTTP_RAW_POST_DATA);
fclose($fh);
?>
One last caveat is when we upload in the aforementioned fashion, the PHP script needs to read from the $HTTP_RAW_POST_DATA variable instead of one of the $_POST or $_GET variables. This example takes the posted XML data and writes family.xml from with it.That's pretty much most of what you will work with when using XML in Actionscript 3 although there are more methods to work with. You can find the XML documentation on the Adobe Flex 2 Language Reference. A complete reference for using XML in Actionscript 3 can also be found at the ECMA 357 Standard (E4X).








SUBSCRIBE


I have done combinations of that AND URLVariables before for various reasons.
<pre>
header("Content-Type: application/xml; charset=ISO-8859-1");
</pre>
You can use whatever charset is appropriate.
var loader:URLLoader = new URLLoader(new URLRequest("family.xml"));
loader.addEventListener(Event.COMPLETE, xmlDownloaded);
function xmlDownloaded(event:Event):void {
var xmlData:XML = new XML(event.target.data);
var surname:String= xmlData.@surname;
var location:String= xmlData.@location;
// get the first granddaughter's age of the first daughter
var grandchild_age:Number= Number(xmlData.DAUGHTER.GRANDCHILD.@age);
// search through all elements inside FAMILY
for each (var object:XML in xmlData.*) {
if (object.name() == "SON") {
var bday:String= object.@bday;
trace (bday);
} else
if (object.name() == "GRANDCHILD") {
var age:Number= object.@age; // will automatically convert to Number
} else
if (object.name() == "DAUGHTER") {
var num_grandchildren:uint= object.*.length();
trace (num_grandchildren);
}
}
// search through all child nodes of DAUGHTER
for each (var grandchildren:XML in xmlData.DAUGHTER.*) {
var ageStr:String= grandchildren.@age;
}
}
But, assume you have an arithmetic expression as a node content. how do you evaluate and return result instead of a string (b'cos: trace(node.exp) will return a string not the result of evaluation - see xml below)
<node>
<exp>10 + 5 * 20</exp>
<node>
Thanks for the library. Its not actually for the stated type of problem. But, I'm still on a way out for a solution.
Toks
But: say I want to allow users to view a list of images from database and to upload their images to db. I'm using Flex,grails and mysql. Any idea will be welcome.
Thank you,
kwame.
he says it will do this but how it goes there is anyones guess.
Thanks for the effort - but would be great if you checked the code before posting.
i have a xml :
<xm>
<user>Andy</user>
<pass>123</pass>
</xml>
how to i add new data in this xml?
tq..
I corrected the syntax errors and uploaded the xml and php files to my server and ran the fla file on my system. All seems to be working fine but there is no change happening to the xml file at all.
Please help! I need such a code for an important project I'm working on...
Aditya
I don't know old this post is or if you are still answering questions. I was glad to find it, though,
so that I could use Flash to read and then selectively update an existing XML file. Thank you very
much.
I'm having 2 issues:
1) After Flash successfully updates the XML, refreshing the page on the browser and searching/displaying
the changed element(s) still gets me the old info, even though I can actually see the new info within
the XML file itself. I need to close the browser and open a fresh copy to clear the cache. Any tips on
how I can address this with a simple refresh would be greatly appreciated.
2) After opening a fresh copy of the browser, the Flash program cannot successfuly complete the load of
XML file. It seems that extraneous data, about the last 15 lines or so of the XML file, have been
appended at the end after the closing root element tag. Therefore, the XML file is not properly
formatted and can't be loaded in Flash. I can't for the life of me figure out how this happens.
I know this may be difficult to follow. I could email abbreviated versions of the files if you offer to
take a quick look.
My Actionscript code follows your example and I've changed the PHP slightly:
<html>
<body>
<?php
$fh = fopen("testXML2.xml", 'r+') or die("can't open file");
fwrite($fh, $GLOBALS['HTTP_RAW_POST_DATA']);
fclose($fh);
?>
</body>
</html>
I would love to get to the bottom of this soon. Thank you for your time and consideration.
http://willperone.net/Code/as3nocache.php
Basically just append ?r=random number or timestamp to the url and it won't cache. I'm not sure what the extra data you are talking about is about but feel free to email me: will@andrograde.com
<content>
<item image="Abigail_call.jpg" name="Abigail" state = "New York">
<value>ms342</value>
<value>ks567</value>
</item>
<item image="Adam_foldes.jpg" name="Adam" state = "New Jersey" >
<value>fg345</value>
<value>ks749</value>
</item>
<item image="Amanda_falco.jpg" name="Amanda" state = "California">
<value>ms346</value>
<value>ks767</value>
<value>ksty</value>
</item>
</content
so I want to access all <value> nodes and search through them. I know how to search if there is ony one node but I am unable to do the multiple same name node.
Also when i tried to run the XML example with your xml and As3, I got htis error" Scene 1, Layer 'Layer 1', Frame 1, Line 30 1084: Syntax error: expecting identifier before colon" am not very good at AS3, so sorry if iam asking something stupid.
Please reply. Thanks a lot in advance
for each (var value:XML in item.value) {
}
'm new to flex.. i wanna create one task n tiz.. any 1 help me.. plz contact me through mail..
Login - Password Assignment --- >> The first screen should have a small form for username and password and a submit button. The application should access an XML file having the username and password list for various users. Check for the validity of the username and password from the XML file entered by the user and redirect him to the user screen if the credentials are valid. If the credentials are not valid, a message should be displayed saying the credentials are incorrect.
var xmlData:XML = new XML(event.target.data);
var surname:String= xmlData.@surname;
var location:String= xmlData.@location;
// get the first granddaughter's age of the first daughter
var grandchild_age:Number= Number(xmlData.DAUGHTER.GRANDCHILD.@age);
// search through all elements inside FAMILY
for each (var object:XML in xmlData.*) {
if (object.name() == "SON") {
var bday:String= object.@bday;
trace (bday);
} else
if (object.name() == "GRANDCHILD") {
var age:Number= object.@age; // will automatically convert to Number
} else
if (object.name() == "DAUGHTER") {
var num_grandchildren:uint= object.*.length();
trace (num_grandchildren);
}
}
// search through all child nodes of DAUGHTER
for each (var grandchildren:XML in xmlData.DAUGHTER.*) {
var ageStr:String= grandchildren.@age;
}
}
plz help me... 'm new to flex..........
Login - Password Assignment --- >> The first screen should have a small form for username and password and a submit button. The application should access an XML file having the username and password list for various users. Check for the validity of the username and password from the XML file entered by the user and redirect him to the user screen if the credentials are valid. If the credentials are not valid, a message should be displayed saying the credentials are incorrect.
Народ посоветуйте хорошую диету так чтоб особо не напрягаться но и чтоб интересно было?
<xsd:element name="Query" type="tns:xn_res_Mngr_dashboardQuery" xmlns:tns="http://www.niku.com/xog/Query" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
and need to replace tns in type attribute with the value of the attribute named xmlns:tns.
Can you help me out in this???
Sorry for my poor english.
I have this xml file :
<?xml version="1.0" encoding="UTF-8" ?>
<datas>
<data variable="testing" value="true" cast="Boolean" />
<data variable="day" value="Monthday" cast="String" />
</datas>
I want to save modified value, but i don't know the syntax for modifying these value.
I have this script for save :
public function SaveSettings():void {
var outputString:String = '<?xml version="1.0" encoding="utf-8"?>\n';
datasXML = <settings/>;
if(testNeeded == true) {
datasXML.data.variable[testing].@value = 'true';
} else {
datasXML.data.variable[testing].@value = 'false';
}
outputString += datasXML.toXMLString();
outputString = outputString.replace(/\n/g, File.lineEnding);
stream = new FileStream();
stream.open(datasFile, FileMode.WRITE);
stream.writeUTFBytes(outputString);
stream.close();
}
But, the result is not what i want. Thes lines is in defect :
datasXML.data.variable[testing].@value = 'true';
datasXML.data.variable[testing].@value = 'false';
If anyone have the good syntax, i'm interrested.
Thank in advance for your help
datasXML.data[@variable = 'testing'].@valeur = 'false';
private var settingsXML:XML; // THE XML
public var testing:Boolean; // Variable needing value
for(var nameVariable:String in xmlFile.data)
{
if(xmlFile.data.@cast[nameVariable] == 'Number')
{
this[xmlFile.data.@variable[nameVariable]] = Number(xmlFile.data.@value[nameVariable]);
}
if(xmlFile.data.@cast[nameVariable] == 'Boolean')
{
if(xmlFile.data.@value[nameVariable] == 'true')
{
this[xmlFile.data.@variable[nameVariable]] = true;
} else {
this[xmlFile.data.@variable[nameVariable]] = false;
}
}
if(xmlFile.data.@cast[nameVariable] == 'String')
{
this[xmlFile.data.@variable[nameVariable]] = String(xmlFile.data.@value[nameVariable]);
}
}