Sélectionner un fichier local XML et l'enregistrer au format GEXF après traitement
Bonjour,
Je cherche à sélectionner mon fichier XML, lancer la transformation et l'enregistrer au format GEXF.
Pour cela en cherchant j'ai trouvé une fonction réalisée par Watilin sur cette discussion:
je vous poste sa fonction
Citation:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| <!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8" />
<title>Ouvrir, modifier, enregistrer un fichier</title>
<style>
textarea {
display: block;
border: solid thin #6cf;
padding: 1px;
width: 100%;
max-height: 24em;
overflow: auto;
}
</style>
</head>
<body>
<input type="file" />
<script> "use strict";
const regex = /<content>\s*{Nom_produit}\s*<\/content>/g;
function highlightText($textarea) {
$textarea.focus();
let match = regex.exec($textarea.value);
if (match) {
let j = regex.lastIndex;
let i = j - match[0].length;
$textarea.setSelectionRange(i, j);
regex.lastIndex = 0;
}
}
document.querySelector("input[type=file]")
.addEventListener("change", (event) => {
let file = event.target.files[0];
if (!file) {
console.log("Aucun fichier sélectionné");
return;
}
else {
console.log(file);
let reader = new FileReader();
reader.onload = () => {
let $ta = document.querySelector("textarea");
let $button = document.querySelector("button");
if (!$ta) {
$ta = document.createElement("textarea");
$ta.cols = 80;
$ta.rows = 15;
$button = document.createElement("button");
$button.textContent = "Enregistrer";
document.body.append($ta, $button);
$button.onclick = () => {
let blob = new Blob([ $ta.value ], { type: "text/xml" });
let blobUrl = URL.createObjectURL(blob);
let $a = document.createElement("a");
$a.download = file.name;
$a.href = blobUrl;
document.body.append($a);
$a.click();
$a.remove();
URL.revokeObjectURL(blobUrl);
};
}
$ta.value = reader.result;
highlightText($ta);
};
reader.readAsText(file);
}
});
</script>
</body>
</html> |
Je vais l'étudier pour voir si je peux l'adapter à mon besoin car c'est exactement le principe que je recherche d'ouverture de document, et d'enregistrement.
Je pense qu'il faudrait modifier le principe d'édition du document pour qu'il soit fait automatiquement par les fonctions appelées par le script, et le principe d'enregistrement, qu'il ne soit pas au format XML mais au format GEXF.
Je vous poste mon script qui récupère les informations de mon fichier XLM et qui me recréer mon fichier à enregistrer au format GEXF:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
| <!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Lecture arborescence fichier XML</title>
<meta name="Author" content="NoSmoking">
<meta name="DVP-discussion" content="d1946638">
<meta name="DVP-version" content="2.0">
<style>
html, body {
margin: 0;
padding: 0;
font: 1em/1.25 Verdana,sans-serif;
}
main {
display: block;
margin: 0 auto;
padding: 0;
max-width: 60em;
}
h1, h2, h3 {
margin: .5em 0;
color: #069;
}
pre {
margin: 0 0 .5em;
border: 1px solid #CCC;
text-shadow: 1px 1px 0 #FFF;
background: #EEE;
white-space: pre-wrap;
}
</style>
</head>
<body>
<main>
<header>
<h1>Résultat récursif</h1>
</header>
<section>
<h2>Résultat</h2>
<pre id="result"></pre>
</section>
<section>
<h2>Ressources</h2>
<p><a href="https://gephi.org/gexf/format/index.html">https://gephi.org/gexf/format/index.html</a></p>
</section>
</main>
<script>
"use strict";
/* si utilisé avec innerHTML
var TAB = " ";
var CR = "<br>";
var LT = "<";
*/
/* si utilisé avec textContent */
var TAB = " ";
var CR = "\n";
/**
* Gestion des relations pour <edges>
*/
var IDRelation = {
indice: 0,
edge: [],
getID: function(idxParent, elem) {
var previousId = this.indice; // id précédente
this.indice += 1;
// stoquage donnée
this.edge.push({
"id": previousId
,"source": idxParent
,"target": this.indice
,"sourceTag": elem.parentNode.tagName // pour vérif
,"targetTag": elem.tagName // pour vérif
});
return (this.indice);
}
};
/**
* version 2.0
*/
function getNodes(elem, indent, indiceParent) {
var retour = "";
var children = elem.childNodes;
var nbChildren = children.length;
var child;
var id;
var i;
for (i = 0; i < nbChildren; i += 1) {
child = children[i];
// si élément est de type ELEMENT_NODE
if (1 === child.nodeType) {
// récup. ID et mise à jour table liaison
id = IDRelation.getID(indiceParent, child);
// si des enfants
if (child.childElementCount) {
// prise en compte du tagName
retour += indent + TAB + '<node id="' + id + '" label="' + child.tagName + '">' + CR;
retour += indent + TAB + '</node>' + CR;
// on y retourne pour la suite
retour += getNodes(child, indent, id /* indiceParent */ );
}
else {
// récup. attributes si existent pour affichage
if (child.hasAttributes()) {
var attrs = child.attributes;
var item;
var text = [];
for (item = 0; item < attrs.length; item += 1) {
text.push(attrs[item].name + " = " + attrs[item].value);
}
child.textContent = text.join(", ");
}
retour += indent + TAB + '<node id="' + id + '" label="' + child.tagName + '">' + CR;
retour += indent + TAB + TAB + "<attvalues>" + CR;
retour += indent + TAB + TAB + TAB + '<attvalue for="0" value="' + child.textContent + '"/>' + CR;
retour += indent + TAB + TAB + '</attvalues>' + CR;
retour += indent + TAB + '</node>' + CR;
}
}
else {
/* on ne fait rien */
}
}
// fin balise si existe
//var endTag = elem.tagName ? (indent.substring(TAB.length) + LT +"/" + elem.tagName + ">" + CR) : "";
return retour;
}
/* Création de l'entête du document GEXF */
function getEntete(indent) {
indent = indent || '';
var text = [];
text.push('<?xml version="1.0" encoding="UTF-8"?>');
text.push('<gexf xmlns="http://www.gexf.net/1.2draft" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.gexf.net/1.2draft http://www.gexf.net/1.2draft/gexf.xsd" version="1.2">');
text.push(indent + TAB + '<graph defaultedgetype="directed">');
text.push(indent + TAB + TAB + '<attributes class="node">');
text.push(indent + TAB + TAB + TAB + '<attribute id="0" title="Value" type="string"/>');
text.push(indent + TAB + TAB + TAB + '<default>true</default>');
text.push(indent + TAB + TAB + '</attributes>');
return text.join(CR);
}
function getEdges(indent) {
indent = indent || '';
var data = IDRelation.edge;
var text = [indent + '<edges>'];
data.shift(); // supprime le premier non concerné
data.forEach(function(obj, ind) {
text.push(indent + TAB + '<edge id="' + obj.id + '" source="' + obj.source + '" target="' + obj.target + '"/>');
//text.push(indent + TAB + '<edge id="' + obj.id + '" source="' + obj.sourceTag + '" target="' + obj.targetTag + '"/>');
});
text.push(indent + '</edges>');
return text.join(CR);
}
/* Fermeture des balises <graph> et <gexf> */
function getEmpied(indent) {
indent = indent || '';
var text = [];
text.push(indent + TAB + '</graph>');
text.push(indent + '</gexf>');
return text.join(CR);
}
/**
*
*/
function fctCallBack(xml) {
var xmlDoc = xml.responseXML;
var indent = TAB + TAB;
var text = [];;
text.push(getEntete());
text.push(indent + "<nodes>" + CR + getNodes(xmlDoc, indent) + indent + "</nodes>");
text.push(getEdges(indent));
text.push(getEmpied());
document.getElementById("result").textContent = text.join(CR);
}
/**
* c'est parti ...
*/
var url = "data-XML.xml"; // mettre ici ton url
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (4 === this.readyState) { /* DONE */
if (200 === this.status) { /* LOADING */
fctCallBack(this);
}
else {
console.warn("Erreur %d lors du chargement du fichier %s !", this.status, url);
}
}
};
xmlhttp.open("GET", url, true);
xmlhttp.send();
</script>
</body>
</html> |
Pensez vous qu'il est possible de combiner les deux scripts pour n'en faire qu'un seul. Je continuerai à analyser le script Watilin demain matin.
Merci d'avance.