Looking up entries in an XML file

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

 

 

 

Be the first to reply