Copy link to clipboard
Copied
Hello, in flash we was able to load text in dynamic text field from external .XML file. Does anyone know how can I do that in Adobe Animate?
var r = new XMLHttpRequest();
r.addEventListener("load", rF);
r.open("GET", "path/file.xml", true);
r.send();
function rF(e) {
alert(e.target.response);
}
Copy link to clipboard
Copied
html5/canvas or as3?
Copy link to clipboard
Copied
Yes HTML, can pls help me with that?
Copy link to clipboard
Copied
var r = new XMLHttpRequest();
r.addEventListener("load", rF);
r.open("GET", "path/file.xml", true);
r.send();
function rF(e) {
alert(e.target.response);
}
Copy link to clipboard
Copied
Thanks a lot, can you tell me the code to load the text in the txt field? Do you got any idea how can I choose which node to be loaded from the XML file?
Copy link to clipboard
Copied
if you have a node named filename:
function rF(e){
parser = new DOMParser();
var xml = parser.parseFromString(e.target.response, "text/xml");
alert(xml.getElementsByTagName("filename")[0].childNodes[0].nodeValue);
}
Copy link to clipboard
Copied
Thanks a lot. That is great. I had problem to load that text in the text field. I am sorry for asking again but what is the code to load that node text in the text field?
Copy link to clipboard
Copied
function rF(e){
parser = new DOMParser();
var xml = parser.parseFromString(e.target.response, "text/xml");
your_tf.text = xml.getElementsByTagName("filename")[0].childNodes[0].nodeValue;
}
Copy link to clipboard
Copied
so if the name of the text field is "alert" that will work? The problem is that I can not make the text field to load that value taken from the XML?
Copy link to clipboard
Copied
Thanks a lot
Copy link to clipboard
Copied
you're welcome.
Copy link to clipboard
Copied
We did improve what you send: here how it looks like. Now you can load only once the XML file and later by using _this.xml you can load any content in the animation without reloading the XML file. Cheers!
var _this = this;
_this.r = new XMLHttpRequest();
_this.r.addEventListener("load", rF);
_this.r.open("GET", "test.xml", true);
_this.r.send();
function rF(e) {
if(typeof _this.xml === "undefined"){
parser = new DOMParser();
_this.xml =parser.parseFromString(e.target.response, "text/xml");
}
_this.example.text = _this.xml.getElementsByTagName("to")[0].childNodes[0].nodeValue
}
Copy link to clipboard
Copied
Could I possibly see the structure of the XML file so I can understand how to structure mine as cannot seem to get text and get a 'null' alert response.
Thanks
Copy link to clipboard
Copied
Try this for HTML5 How to Load XML with JavaScript on an HTML5 Page - dummies
Try this for AS3 External HTML file into Dynamic Text Field in Flash/AS3 - Stack Overflow
Copy link to clipboard
Copied
I wonder for many things including why this works for me only in the preview window - directly opened by Animate.
As I open exported HTML file in the browser - that external content doesn't show up anymore. Why?
I'm on the way to create the page where the Name on the screen will be chosen randomly from an external text file. It works less or more ok in AS but I can't figure out how to move it to HTML canvas.
Copy link to clipboard
Copied
what code are you using?
Copy link to clipboard
Copied
The same from here marked as the correct answer. It does the thing only when hit cmd+Enter. After opening the HTML file from the disk none of the external content loads.
Copy link to clipboard
Copied
what do you see when testing in animate?
what do you see when opening the html from your hard drive?
Copy link to clipboard
Copied
When testing from Animate it shows some kind of pop-up window with the content of the external file and close button.
When I open rendered HTML file it just skips that place, just shows nothing as there is no any script - nothing. (all files are in the same folder, of course)
Copy link to clipboard
Copied
create a new folder
save your fla to that folder
move the path folder (that contains file.xml) to that new folder
publish to that folder
test in animate.
test by opening the html in that new folder.
any problem?
Copy link to clipboard
Copied
Does it work for everybody else? What I'm doing wrong?
Here are my files ...
Copy link to clipboard
Copied
And there is another one strange thing:
In my folder are 4 files:
- project.fla
- names.txt
- index.html and -index.js (those 2 are rendered from project .fla export)
If I rename "names.txt" file to "names.xml" and open project.fla again it will preview the correct movie with loaded external file data even there is no that file anymore. It doesn't need to rewrite the source in the code. What this could mean?
Copy link to clipboard
Copied
then you're not using the code i suggested in the answer marked as correct.
so, copy (from your fla) and paste (here) the code you're using to load the xml file.
Copy link to clipboard
Copied
var r = new XMLHttpRequest();
r.addEventListener("load", rF);
r.open("GET", "Names.txt", true);
r.send();
function rF(e) {
alert(e.target.response);
}
Copy link to clipboard
Copied
the file you want to load should be renamed to Names.txt (and you'll probably have a cross-domain security issue (Same-origin policy - Wikipedia) if you try to open that text file on your local (computer) system. ie, upload the files to a server and test.