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
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libxml/parser.h>
#include <libxml/xpath.h>
int main() {
//Ouverture d'un fichier txt pour sauvegarder les données xml recupérées
char test[3000];
double test2;
// Ouverture du document XML
xmlKeepBlanksDefault(0); // Ignore les noeuds texte composant la mise en forme
xmlDocPtr doc = xmlParseFile("donnees.xml");
if (doc == NULL)
{
fprintf(stderr, "Document XML invalide\n");
return EXIT_FAILURE;
}
/ Initialisation de l'environnement XPath
xmlXPathInit();
// Création du contexte
xmlXPathContextPtr ctxt = xmlXPathNewContext(doc);
if (ctxt == NULL)
{
fprintf(stderr, "Erreur lors de la création du contexte XPath\n");
return EXIT_FAILURE;
}
// Evaluation de l'expression XPath
xmlXPathObjectPtr xpathRes = xmlXPathEvalExpression("/region/regionsphere/elements/text()", ctxt);
if (xpathRes == NULL)
{
fprintf(stderr, "Erreur sur l'expression XPath\n");
return EXIT_FAILURE;
}
// Manipulation du résultat
xmlNodePtr tab;
if (xpathRes->type == XPATH_NODESET)
{
int i;
for (i = 0; i < xpathRes->nodesetval->nodeNr; i++) {
xmlNodePtr n = xpathRes->nodesetval->nodeTab[i];
if (n->type == XML_TEXT_NODE || n->type == XML_CDATA_SECTION_NODE)
{
tab=n->content;
printf("%s\n", tab); }
sscanf (tab, "%600c" , test) ;
printf("test =%s\n", test);
sscanf (test, "%c" , test2) ;
printf("test2=%f\n",test2);
// test
printf("\n test0 =%c\n", test[0]);
printf("test1 =%c\n", test[1]);
printf("test2 =%c\n", test[2]);
printf("test3 =%c\n", test[3]);
xmlXPathFreeObject(xpathRes);
xmlXPathFreeContext(ctxt);
xmlFreeDoc(doc);
return EXIT_SUCCESS;
} |
Partager