Skip to main content
Kamil.Ch
Inspiring
Dec 13, 2023 10:33 AM UTC
Answered

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>

This topic is closed to new replies. Start a new post to keep the conversation going.
kglad
kgladCorrect answer
Community Expert
Dec 13, 2023 2:32 PM UTC

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

 

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

5 replies

kglad
Community Expert
Community Expert
Dec 13, 2023 2:20 PM UTC

what's the problem you see?

Translate
Kamil.Ch
Kamil.ChAuthor
Inspiring
Dec 13, 2023 2:22 PM UTC

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

Translate
Kamil
kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
Dec 13, 2023 2:32 PM UTC

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

 

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

Translate