conversion xml

by toubia95

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/downloadjs/1.4.8/download.min.js"></script>
<p>
Cet outil permet de convertir en TXT ou en XML-TEI les fichiers libres de droits téléchargés depuis Frantext (www.frantext.fr).<br><br>
1. <input type="file" id="allegro" name="filename"><br>
2. Formats de conversion : 
  <button class="b" type="button" id="txt">
  <a href="#" class="ab">TXT »</a>
</button>
<button class="b" type="button" id="xmltei"  disabled>
  <a href="#" class="ab">XML-TEI Simple »</a>
</button>
  <button class="b" type="button" id="etiquet"  disabled>
  <a href="#" class="ab">XML-TEI Etiqueté »</a>
</button>
</p>
<div id="status"></div>

CSS

.ab {
  color: black;
  text-decoration: none;
}
.erreur {
  color: red;
}
.succes {
  color: green;
}

JavaScript

// https://developer.mozilla.org/fr/docs/Web/API/File/Using_files_from_web_applications

// https://ourcodeworld.com/articles/read/189/how-to-create-a-file-and-generate-a-download-with-javascript-in-the-browser-without-a-server
//https://stackoverflow.com/questions/750032/reading-file-contents-on-the-client-side-in-javascript-in-various-browsers

// https://github.com/rndme/download

// https://developer.mozilla.org/en-US/docs/Web/API/FileReader/result

// https://stackoverflow.com/questions/12448595/get-set-file-encoding-with-javascripts-filereader

$("#txt").on("click",function() {
 $('#status').html("");
});
$("#txt").on("click",function() {
$('#status').html("");
var allegro = $('#allegro')[0].files[0];
if (allegro) {
 var n1 = allegro.name;
 var n2 = n1.replace('.xml','');
 var txtname = n2+".txt";
 var teiname = n2+".tei.xml";
 var etiname = n2+".annote.xml";
 var reader = new FileReader();
    reader.readAsText(allegro, "ISO-8859-1");
    //reader.readAsText(allegro, "UTF-8");
    reader.onload = function (e) { // async ?
        var contentxml = e.target.result;
        console.log(contentxml);
        //var utf = decodeEntities(contentxml);
        // var utf = contentxml.replace(/<\/l>/g,"$1\n"); // bugs à cause de l'encodage
        $('#status').html("<p class='succes'>Traitement du fichier "+n1+"...</p>");
        /* console.log(contentxml);
        var text = contentxml.replace(/<\?xml(.|\n)*?<text>/gi,"");
        var text = text.replace(/<pb n="(.+)"\/>/gi,"\n\n$$$1\n\n");
        var text = text.replace(/<[^>]+>/gi,"");
        var text = text.replace(/\n(\n| )+\n/gi,"\n\n"); */
        // download(contentxml, txtname, "text/plain;charset=utf-8"); //text/xml
        download(contentxml, teiname, "application/xml;charset=utf-8");
    }  
    reader.onerror = function (e) {
        $('#status').html("<p class='erreur'>Erreur de lecture du fichier !</p>");
    }
} else {
 $('#status').html("<p class='erreur'>Aucun fichier n'a été choisi ! Utilisez le...