Looking up entries in an XML file

May 20, 2019

Hi,

I am creating a custom online glossary for our courses, using glossary files that are in XML format.

Searching the forums, the only relevant discussions I can find are from some years ago and don't really cover what I need to do. So I thought if I posted what I have got working so far, some of you might be able give me some tips and hopefully it will be useful for anyone trying to get started working with XML files.

The example Storyline file attached has the toolbar on a slide master. When you enter a term in the glossary field and click the search button, any matching glossary entry in the xml file will be returned with the definition (examples in the attached abridged glossary include 'accident' and 'risk').

You will need to place the xml file in the root folder of the published zip file. The lookup will only work when uploaded onto your LMS.

The following javascript is used:

const xhttp = new XMLHttpRequest(); // we are going to get an xml file
xhttp.onreadystatechange = function() { // when the file is ready to read then do the look up function
if (this.readyState == 4 && this.status == 200) {
lookupDefn(this); // call the look up function
}
};
xhttp.open("GET", "glossary.xml", true); // get the xml file
xhttp.send();

function lookupDefn(xml) {
const player = GetPlayer(); // get the Storyline player
let lookupWord = player.GetVar("TextEntry"); // get the look up word that has been entered
const xmlDoc = xml.responseXML; // get the xml content from the file
let indx, title, comment; // declare some handy variables
let cases = xmlDoc.getElementsByTagName("CASE"); // an array of all the entries so we can check how many there are

for(indx=0; indx<cases.length; indx++) { // loop through all the entries
title = xmlDoc.getElementsByTagName("TITLE")[indx].childNodes[0].nodeValue; // get the title of the entry
if(title.toLowerCase()===lookupWord.toLowerCase()) {break;} // check if the title matches the lookup word
}

comment = (indx<cases.length) ? xmlDoc.getElementsByTagName("COMMENT")[indx].childNodes[0].nodeValue : "No glossary entries found."; // this ternary operator gets the comment (definition) if an entry was found
title = (indx<cases.length) ? title : " "; // this ternary operator gets the title if an entry was found
player.SetVar("LookupTitle", title); // set the title variable in the Storyline player
player.SetVar("LookupComment", comment); // set the comment variable in the Storyline player
}

I would greatly appreciate any feedback on the javascript as I am a novice. In particular I am not sure why I cannot get the fetch() method to work.

Developments from here will be to match search terms on the keywords in the XML file as well as the title.

I would like to match on part of the title/keyword (using the .includes() method) but the problem is how to handle multiple matches.

Many thanks

 

 

 

4 Replies
Dave Cox

Hi Daniel,

To start with, the commands that you are referring to are actually ajax commands. Ajax commands are special subset of JavaScript commands the call for server callback functions. These can sometimes be tricky at best to get to work, and I think that they are probably over kill to just pull in an XML file, unless your server is going to be continually updating the file. 

Ajax command are typically used for server call back functions where maybe you want to query a database, but you don't want to completely redraw the users browser window to update the information. Also, as you noticed, there is no way to know if Ajax functions are working properly unless you can actively talk to the server, which in this case is the LMS. 

If you only need to read the contents of the XML file when the course starts, then a much better solution is just use JavaScript's normal XML tools, starting with the SMLHttpRequest Object and the XML Parser. Although it has been quite a long time, I've successfully done this with Storyline, although I suggest you perfect your scripts in a browser first. This way, you don't have to upload it to the LMS to test it every time. Once you have your scripts working in a browser, then you can modify them where you can call them from Storyline.

Probably the best place is to start here

If I have enough time this weekend, I'll try to put something together for you.

Dave Cox

Hi Daniel,

Here is some code that can extract the information in your XML file, and display it on a page using just Javascript. It wouldn't be hard to modify the javascript to send the code to Storyline.

If you want to test it as it is, I recommend you download USBWebserver. That will let you create a virtual webserver in a folder, or on a USB drive. It's great for testing.

Daniel Kerr

Hi Dave,

Thank you so much for this. I have been off work for a few months so only just picking this up now.

I will have a good look through what you have directed me to and the example you have provided, it looks like the route to a better understanding of what I am trying to do here.

Lauri I will post an update when I get something working.

This discussion is closed. You can start a new discussion or contact Articulate Support.