Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Canvas HTML5 importing xml text file

Explorer ,
Dec 13, 2023 Dec 13, 2023

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>

TOPICS
Code , Import and export
474
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Dec 13, 2023 Dec 13, 2023

what's the problem you see?

Translate
Community Expert , Dec 13, 2023 Dec 13, 2023

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

 

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

Translate
Community Expert ,
Dec 13, 2023 Dec 13, 2023

what's the problem you see?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 13, 2023 Dec 13, 2023

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 13, 2023 Dec 13, 2023

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

 

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 13, 2023 Dec 13, 2023

Thank you very much, your help is very useful.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 14, 2023 Dec 14, 2023
LATEST

you're welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines