Copy link to clipboard
Copied
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>
2 Correct answers
what's the problem you see?
the use of "this" in transferComplete is the first error i see. ie, use
req.addEventListener("load", transferComplete.bind(this));
Copy link to clipboard
Copied
what's the problem you see?
Copy link to clipboard
Copied
The project is published correctly. It does not load text into dynamic fields from an xml file.
Copy link to clipboard
Copied
the use of "this" in transferComplete is the first error i see. ie, use
req.addEventListener("load", transferComplete.bind(this));
Copy link to clipboard
Copied
Thank you very much, your help is very useful.
Copy link to clipboard
Copied
you're welcome.

