0

I've started programming with Javascript only a few days ago.

I need to open a .xml file, read some of the tags in there and store them in an array.

I'v tried with the fetch function (even if I don't understand how it works):

fetch('fileName.xml')
        .then(function (response) {
            console.log(response);
            return response.json();
        })
        .then(function (data) {
            //do what you want
        });

I think this is the right structure for the code, but I don't know hot to continue. How can I read some of the tags in my file?

Thank you in advance for you answers!

ps: I'm programming on browser

Maria
  • 1
  • 2
  • 1
    Why are you calling the `json()` function when you want to read XML? – mousetail Jul 13 '22 at 14:03
  • 1
    Use `.text()` instead of `.json()`, then use some parser to parse the data – qrsngky Jul 13 '22 at 14:04
  • Ok I tried with .text() and now I can read the data in the file. I created a string with all the data I need to take from the fileName.xml and now I would like to save the string in a file and download it. Is it possible? – Maria Jul 13 '22 at 14:26
  • You may convert your text to Data URI (reference: https://stackoverflow.com/questions/63025140/making-a-data-uri-from-unicode-string ) and then download the data uri (reference: https://stackoverflow.com/questions/3916191/download-data-url-file ) – qrsngky Jul 13 '22 at 14:29
  • Does this answer your question? [Parse XML using JavaScript](https://stackoverflow.com/questions/17604071/parse-xml-using-javascript) – evolutionxbox Jul 13 '22 at 17:35

0 Answers0