简体   繁体   中英

managing of xml nodes in actionScript 3.0

I don't know very much of xml and sadly I don't have much time to learn about.

I've created a php file that connects to a sql database and writes the fields

in an internal xml structure:

 <?PHP

$link = mysql_connect("localhost","root","");
mysql_select_db("myDatabase");
$query = 'SELECT * FROM users';
$results = mysql_query($query);

echo "<?xml version=\"1.0\"?>\n";

echo "<user>\n";

while($line = mysql_fetch_assoc($results)) {

    echo "<item>" . $line["user"]."</item> \n";

}


echo "</user>\n";

mysql_close($link);

?>

this works but I need to read it from a swf file:

var myXML:XML;
var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest("http://localhost/users.php"));
myLoader.addEventListener(Event.COMPLETE, processXML);
function processXML(e:Event):void {
    myXML = new XML(e.target.data);
    trace(myXML);
    mytext.text=myXML;
}

how can I manage the users from flash? for example if I want to manipulate the first user (user[0]) how can I do it?

I've tried to add this:

trace(myXML.user);
        mytext.text=myXML.user;

but it doesn't work, i've tried to find in internet but this argument is too much articulated. Thanks

You have XML of structure:

<user>
    <item>Ann</item>
    <item>Alice</item>
    <item>Bob</item>
    <item>Cecile</item>
</user>

Variable myXML from your code points to the root node, which is user . Now, to address items you need to:

// Get all child nodes of the root, whose node name is "item".
var aList:XMLList = myXML.child("item");
// You can also write it = myXML.item; but I don't like this way
// because of possible confusion with XML class methods.

// Now you can access list items which are XML nodes.
// Note that XMLList.length() is a method, not a getter property.
for (var i:int = 0; i < aList.length(); i++)
{
    var anItem:XML = aList[i];

    // Basically you get all anItem children as XMLList
    // which consist of a single text XML node,
    // and convert it into a string.
    var aName:String = anItem.children().toXMLString();

    trace(i, aName);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM