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 198 199 200 201 202 203 204 205 206
| <!DOCTYPE html>
<html lang="fr">
<body>
<main>
<h1> "Script réalisé avec l'aide reçue par les développeurs de developpez.net"</h1>
<pre id="result"></pre>
</main>
<script>
/* Déclaration des variables */
var TAB = " ";
var CR = "\n";
var LT = '"';
var ret = "";
var reto = "";
var retou = "";
var lastId = 1;
var lastId1 = 1;
var child, x, y;
var IDs;
var IDt;
var test = "";
/************************************************************************************************************/
/* Récupération et ouverture du document XML */
"use strict";
var url = "Bra.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();
/************************************************************************************************************/
/* Création de l'entête du document GEXF */
function createAttr(elem, indent){
indent += TAB;
ret += "<?xml version=" + LT + "1.0" + LT + "encoding=" + LT + "UTF-8" + LT + "?>" + CR;
ret += "<gexf xmlns=" + LT + "http://www.gexf.net/1.2draft" + LT + " xmlns:xsi=" + LT + "http://www.w3.org/2001/XMLSchema-instance" + LT + " xsi:schemaLocation=" + LT + "http://www.gexf.net/1.2draft http://www.gexf.net/1.2draft/gexf.xsd" + LT + " version=" + LT + "1.2" + LT + ">" + CR;
ret += indent + "<graph defaultedgetype=" + LT + "directed" + LT + ">" + CR + CR;
/* Création et définition de l'attribue value pour les nodes */
ret += indent + "<attributes class=" + LT + "node" + LT + ">" + CR;
ret += indent + indent + "<attribute id=" + LT + "0" + LT + "title=" + LT + "Value" + LT + "type=" + LT + "string" + LT + "/>" + CR;
ret += indent + indent + "<default>true</default>" + CR;
ret += indent + "</attributes>" + CR + CR + CR;
ret += indent + "<nodes>" + CR;
return ret;
}
/************************************************************************************************************/
/* Création d'un identifiant unique */
function getUniqueId() {
return lastId1 ++;
return lastId ++;
}
/************************************************************************************************************/
/* Récupération des noeuds parents et enfants avec affichage de leur valeur */
function getChildren(elem, indent) {
var retour = "";
var children = elem.childNodes;
var nbChildren = children.length;
var i;
indent += TAB;
for (i = 0; i < nbChildren; i += 1) {
child = children[i];
// si élément est de type ELEMENT_NODE
if (1 === child.nodeType) {
x = lastId++;
// si des enfants
if (child.childElementCount) {
// si le noeud n'a pas d'attribut, retourne l'identifiant et le nom du noeud
retour += indent + indent + "<node id=" + LT + x + LT + " " + "label=" + LT + child.tagName + LT + ">" + CR;
retour += indent + indent + "</node>" + CR;
// on y retourne pour la suite (récursivité)
retour += getChildren(child, indent);
} else {
// rédup. 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(", ");
}
// si le noeud a un ou plusieurs attributs, retourne l'identifiant, le nom et l'attribut du noeud
retour += indent + indent + "<node id=" + LT + x + LT + " " + "label=" + LT + child.tagName + LT + ">" + CR
retour += indent + indent + indent + "<attvalues>" + CR;
retour += indent + indent + indent + indent + "<attvalue for=" + LT +"0" + LT + " " + "value=" + LT + child.textContent + LT + "/>" + CR;
retour += indent + indent + indent + "</attvalues>" + CR;
retour += indent + indent + "</node>" + CR;
}
}
else { /* on ne fait rien */ }
}
// fin balise si existe
return retour;
}
/************************************************************************************************************/
/* Fermeture de la balise "nodes" et ouverture de la balise "edge" */
function edge(elem, indent) {
indent += TAB;
retou += indent + indent + "</nodes>" + CR + CR + CR;
retou += indent + indent + "<edge>" + CR;
return retou;
}
/************************************************************************************************************/
/* Récupération des liens entre les noeuds parents et enfants */
function getTarget(elem, indent) {
var children = elem.childNodes;
var nbChildren = children.length;
var j;
indent += TAB;
var retourn = ""
IDs = "Parent";
IDt = "Enfant";
for (j = 0; j < nbChildren; j += 1) {
child = children[j];
// si élément est de type ELEMENT_NODE
if (1 == child.nodeType) {
/* if (child.parentNodes){
IDs = getChildren(elem, indent).x;
}
else
{
IDt = getChildren(elem, indent).x;
}
*/
y = lastId1 ++;
test = TAB + TAB + TAB + "<edge id=" + LT + y + LT + " " + "source=" + LT + IDs + LT + " " + "target=" + LT + IDt + LT + "/>" + CR; // si des enfants
if (child.childElementCount) {
// prise en compte du target
retourn += test;
// on y retourne pour la suite
retourn += getTarget(child, indent);
} else {
retourn += test;
}
}
else { /* on ne fait rien */ }
}
return retourn;
}
/************************************************************************************************************/
/* Fermeture des balises "edges", "graph" et "gexf" */
function closeFile (elem, indent){
indent += TAB;
reto += indent + indent + "</edges>" + CR + CR;
reto += indent + "</graph>" + CR;
reto += "</gexf>" + CR;
return reto;
}
/************************************************************************************************************/
/* Appel des différentes fonctions pour la création du document GEXF */
function fctCallBack(xml) {
var xmlDoc = xml.responseXML;
var txt = createAttr(xmlDoc, "");
var text = getChildren(xmlDoc, "");
var txte = edge(xmlDoc, "");
var texte = getTarget(xmlDoc, "");
var textes = closeFile(xmlDoc, "");
document.getElementById("result").textContent = txt + text + txte + texte + textes;
}
</script>
</body>
</html> |