Canvas HTML5 importing xml text file
I need help downloading text from an external xml file
Animate CC canvas HTML5:
this.stop();
// Load the XML file
var req = new XMLHttpRequest();
req.open("GET", "tekst.xml", true);
req.addEventListener("load", transferComplete);
req.send();
function transferComplete(e) {
// Check if the response from the server has a status of 200 (OK)
if (e.target.status === 200) {
console.log("The XML file has been loaded successfully.");
/ Parser XML
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(e.target.response, "text/xml");
//Return an array of all matching tag values
var textEntries = getTagValues(xmlDoc, "TextEntry");
// Check if there are enough elements in the array
if (textEntries.length >= 3) {
// Transcribe text into text boxes in Animate CC
this.textFile01.text = textEntries[0];
this.textFile02.text = textEntries[1];
this.textFile03.text = textEntries[2];
} else {
console.error("Error: Not enough TextEntry in the XML file.");
}
} else {
console.error("Error: Failed to load XML file. Status: " + e.target.status);
}
}
// Returns an array of all matching tag values
function getTagValues(xml, tag) {
var matches = [];
var nodes = xml.getElementsByTagName(tag);
for (var i = 0; i < nodes.length; i++) {
matches.push(nodes[i].textContent);
}
return matches;
}
XML:
<textFiles>
<TextEntry>11111</TextEntry>
<TextEntry>22222</TextEntry>
<TextEntry>33333</TextEntry>
</textFiles>
