[XML] Upload and Read Files

Hi all!

I need a some help with Upload and read XML files into Weweb…

I look some formulas using DOMParse() but this read a string of file… I need read the file to generate a string…

I look the formula like:


fetch("file_upload.xml")
  .then(response => response.text())
  .then(data => {
    const parser = new DOMParser();
    const xml = parser.parseFromString(data, "application/xml");
    console.log(xml);
  })
  .catch(console.error);

How do I this in Weweb. My goal is extract some data and create a JSON object…

A example is

<Driver>
<Name>Felipe Granado</Name>
<Position>1</Position>
</Driver>

Thanks for all effort!

[UPDATE]

If someone needs to manipulate XML files, follow the code below:

const xhr = new XMLHttpRequest();

xhr.open("GET", url, false);

try
{
    xhr.send(); // Send request

    const xmlDoc = xhr.responseXML; // the XML is loaded
        
    const drivers = xmlDoc.querySelectorAll('Driver');

    const result = Array.from(drivers).map(driver => {
        const driverData = {};

        // Keep all subElements (Name, Position, etc.)
        const subElements = driver.querySelectorAll('*'); // Keep all direct children 

        subElements.forEach(subElement => {
            const tagName = subElement.tagName; // Tag Name
            const value = subElement.textContent; // Content
        
        driverData[tagName] = value; // Add to data Object
    });

    return driverData;
});

    result.sort (( a, b) => a.Position - b.Position );
    
    return result;
}
catch (error)
{
        console.error('Error during request:', error);
}

With this, the output will be:

[
   {
      "Name": "Felipe Granado",
      "Position": "1"
   }
]

To make it better within WeWeb, I created a formula that used the URL of the uploaded file as a parameter.