[Xerte] Having trouble reading XML from an external script
Tenney Julian
Julian.Tenney at nottingham.ac.uk
Wed Aug 29 21:49:30 BST 2007
I'll implement the loadVars option for you, no problem.
What I really like about using xml is that I can get at the de-serialised object straight away (iconID.templateData), as IconID.sendAndLoad gives me both the raw xml (iconID.serverXML), and the de-serialised object, so I can use dot notation to get at the nested xml. I frequently send in data that drives a template in this way, and it is really handy to have everything in one object, since it could be a quiz or more complex piece, with a nested data structure.
If I set the templateData on the LO tag to an xml file (FileLocation + 'data.xml) then the global variable templateData is set before the piece loads (it is the deserialised object), and the data is available immedietely to your code. You can use that data to build the piece at runtime, creating, say, the right number of questions in a quiz, the right number of options for each question, and feedback for each option using the iconID.duplicate([n]) function.
If I then create a .xwd file data.xwd - it must have the same name as the xml file, i.e. 'data' in this case (see help on templates) - I can easily create a wizard to edit the quiz data by defining the wizard's controls in the xwd file. If I change the .rlo file's extension to .glo then when Xerte opens the file, it also opens the wizard. I can change a value in the registry, so that users that don't need the authoring tool, only ever see the wizard. This means they can use the template to easily create as many quizzes as they want.
Authorware's KOs were a very cool idea - but the problem with creating KOs in authorware, is that the wizard is complicated to build. This makes it very easy. Templates created in this way cn then be packaged (through the templates menu) and distributed to other Xerte users.
I'll put some examples online over the next few days - I'm presenting this approach at ALT-C next week, and I'll make some examples available,
Julian
________________________________
From: xerte-bounces at lists.nottingham.ac.uk on behalf of Paul Swanson
Sent: Wed 29/08/2007 19:18
To: Xerte discussion list
Subject: RE: [Xerte] Having trouble reading XML from an external script
I was doing something similar to the PHP suggestion, but not getting anything. Using Thomas' code, I was able to see that the data is getting to PHP, but since it is sent by the POST method, PHP is expecting name/value pairs, separated by ampersands. I struggled for a while in that PHP was returning only part of the string that was sent to it by Xerte, and realized it was breaking at an equal sign. I was using:
myId = 5;
myXML = "<query>SELECT * FROM tablename WHERE id = " + myId + " ORDER BY fname</query>";
myReq = new XML(myXML);
xmlPage.sendAndLoad(myReq, 'http://localhost/test/test_xerte.php');
... and was receiving back only "5 ORDER BY fname". I eventually realized that the string I was getting back was everything after the equal sign. So I amended my string to:
myXML = "type=SELECT&table=tablename&id=" + myId + "&sort=fname";
... and now I can see all of the name/value pairs in PHP and echo them back. I found that I needed to urlencode the strings in PHP before echoing them, and then unescape the returning string in Xerte.
My PHP code is now:
<?php # test_xerte.php
// this script is a test of whether I can connect to a php script through Xerte.
if (!empty ($_POST)) {
echo "<reply>";
foreach ($_POST as $key => $value) {
echo urlencode ("<$key>$value</$key>\n");
}
echo "</reply>";
} else {
echo '<reply>The $_POST array was empty.</reply>';
}
?>
In Xerte, the script that receives that data back from PHP is:
xmlResponse = unescape(xmlPage.templateData.reply);
replyPage.myText = '<p><b>Response from script: </b>';
replyPage.myText += '<i>' + xmlResponse + '</i></p>';
replyPage.myText += "<br /><p>That's all folks!</p><br /><p>Another paragraph.</p>";
So now I see all of the values returned by PHP. The xml tags don't display, of course, but I should be able to figure out how to work with them.
But since you mentioned implementing loadVars, I would like to say that it seems that would be an easier way to communicate with PHP than using XML. It would be similar to how PHP scripts communicate with one another, and would make database communication easier to implement, since you wouldn't have to convert back and forth between name/value style and xml style.
- Paul
________________________________
From: xerte-bounces at lists.nottingham.ac.uk [mailto:xerte-bounces at lists.nottingham.ac.uk] On Behalf Of Tenney Julian
Sent: Wednesday, August 29, 2007 5:22 AM
To: Xerte discussion list
Subject: RE: [Xerte] Having trouble reading XML from an external script
Hi,
Xerte does do the sendAndLoad a little differently as in Xerte sendAndLoad is a method of an icon; in Flash it is a method of an xml object. I did it this way because you need to listen for the event and it is easy to set up listeners to events broadcast by icons in Xerte, rather than objects.
I can implement a loadVars in similar fasion if you want?
To get at the string POSTed into the php page, try this:
$empty = $post = array();
foreach ($_POST as $varname => $varvalue) {
if (empty($varvalue)) {
$empty[$varname] = $varvalue;
} else {
$post[$varname] = $varvalue;
}
}
[Thanks to Thomas]
Julian
More information about the Xerte
mailing list