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
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libxml/tree.h>
#include <libxml/parser.h>
#include "dom_parcours.h"
#include "structure.h"
int i=0;
int var_test;
void parcours_prefixe(xmlNodePtr noeud, fct_parcours_t f) {
xmlNodePtr n;
for (n = noeud; n != NULL; n = n->next) {
//f(n);
f(n);
if ((n->type == XML_ELEMENT_NODE) && (n->children != NULL)) {
parcours_prefixe(n->children, f);
}
}
}
void afficher_noeud(xmlNodePtr noeud) {
if (noeud->type == XML_ELEMENT_NODE)
{
xmlChar *chemin = xmlGetNodePath(noeud);
//printf("nom du noeud courant : %s\n",noeud->name);
if ( (!xmlStrcmp(noeud->name,(const xmlChar *) "lien")) )
{
i++;
printf("le noeud courant est un lien : ");
if (noeud->children != NULL && noeud->children->type == XML_TEXT_NODE)
{
xmlChar *contenu = xmlNodeGetContent(noeud);
//printf("%s -> %s\n", chemin, contenu);
printf(" %s\n",contenu);
xmlFree(contenu);
}
else
{
printf("%s\n", chemin);
}
xmlFree(chemin);
}
else if ( (!xmlStrcmp(noeud->name,(const xmlChar *) "adresse")) )
{
printf("le noeud courant est une adresse : ");
if (noeud->children != NULL && noeud->children->type == XML_TEXT_NODE)
{
xmlChar *contenu = xmlNodeGetContent(noeud);
//printf("%s -> %s\n", chemin, contenu);
printf(" %s\n",contenu);
switch(i)
{
case 1 : link1.address=(char *)contenu;
printf("address of link 1 : %s\n",link1.address);
break;
case 2 : link2.address=(char *)contenu;
printf("address of link 2 : %s\n",link2.address);
break;
case 3 : link3.address=(char *)contenu;
printf("address of link 3 : %s\n",link3.address);
break;
}
xmlFree(contenu);
}
else
{
printf("%s\n", chemin);
}
xmlFree(chemin);
}
else if ( (!xmlStrcmp(noeud->name,(const xmlChar *) "port")) )
{
printf("le noeud courant est un port : ");
if (noeud->children != NULL && noeud->children->type == XML_TEXT_NODE)
{
xmlChar *contenu = xmlNodeGetContent(noeud);
//printf("%s -> %s\n", chemin, contenu);
printf(" %s\n",contenu);
switch(i)
{
case 1 : link1.port=(char *)contenu;
printf("port of link 1 : %s\n",link1.port);
break;
case 2 : link2.port=(char *)contenu;
printf("port of link 2 : %s\n",link2.port);
break;
case 3 : link3.port=(char *)contenu;
printf("port of link 3 : %s\n",link3.port);
break;
}
xmlFree(contenu);
}
else
{
printf("%s\n", chemin);
}
xmlFree(chemin);
}
else if ( (!xmlStrcmp(noeud->name,(const xmlChar *) "routing")) )
{
printf("le noeud courant est une adresse de routage : ");
if (noeud->children != NULL && noeud->children->type == XML_TEXT_NODE)
{
xmlChar *contenu = xmlNodeGetContent(noeud);
//printf("%s -> %s\n", chemin, contenu);
printf(" %s\n",contenu);
switch(i)
{
case 1 : link1.routing=(char *)contenu;
printf("route of link 1 : %s\n",link1.routing);
break;
case 2 : link2.routing=(char *)contenu;
printf("route of link 2 : %s\n",link2.routing);
break;
case 3 : link3.routing=(char *)contenu;
printf("route of link 3 : %s\n",link3.routing);
break;
}
xmlFree(contenu);
}
else
{
printf("%s\n", chemin);
}
xmlFree(chemin);
}
else if ( (!xmlStrcmp(noeud->name,(const xmlChar *) "nom")) )
{
printf("le nom du lien physique est : ");
if (noeud->children != NULL && noeud->children->type == XML_TEXT_NODE)
{
xmlChar *contenu = xmlNodeGetContent(noeud);
printf("%s -> %s\n", chemin, contenu);
switch(i)
{
case 1 : link1.name=(char *)contenu;
printf("name of link 1 : %s\n",link1.name);
break;
case 2 : link2.name=(char *)contenu;
printf("name of link 2 : %s\n",link2.name);
break;
case 3 : link3.name=(char *)contenu;
printf("name of link 3 : %s\n",link3.name);
break;
}
xmlFree(contenu);
}
else
{
printf("%s\n", chemin);
}
xmlFree(chemin);
}
}
var_test=40;
}
int extraire_data() {
xmlDocPtr doc;
xmlNodePtr racine;
/* Initialize structure link */
/*link1.number=1;
link2.number=2;
link3.number=3;
*/
// Ouverture du document
xmlKeepBlanksDefault(0); // Ignore les noeuds texte composant la mise en forme
doc = xmlParseFile("carte.xml");
if (doc == NULL)
{
fprintf(stderr, "Document XML invalide\n");
return EXIT_FAILURE;
}
// Recuperation de la racine
racine = xmlDocGetRootElement(doc);
if (racine == NULL)
{ fprintf(stderr, "Document XML vierge\n");
xmlFreeDoc(doc);
return EXIT_FAILURE;
}
// Parcours
printf("Parcours prefixé :\n");
parcours_prefixe(racine, afficher_noeud);
printf("\n\n");
xmlFreeDoc(doc);
//var_test=29;
printf("name of link 1: %s\n",link1.name);
return EXIT_SUCCESS;
} |
Partager