Skip to main content
Kamil.Ch
Inspiring
December 13, 2023
Answered

Canvas HTML5 importing xml text file

  • December 13, 2023
  • 1 reply
  • 605 views

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>

This topic has been closed for replies.
Correct answer kglad

the use of "this" in transferComplete is the first error i see.  ie, use

 

req.addEventListener("load", transferComplete.bind(this));

1 reply

kglad
Community Expert
Community Expert
December 13, 2023

what's the problem you see?

Kamil.Ch
Kamil.ChAuthor
Inspiring
December 13, 2023

The project is published correctly. It does not load text into dynamic fields from an xml file.

Kamil
kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
December 13, 2023

the use of "this" in transferComplete is the first error i see.  ie, use

 

req.addEventListener("load", transferComplete.bind(this));