Extraire d'un fichier XML un attribut objet
Bonjour,
Après avoir fait les tutoriels sur boost archive, et boost serialization, il s'avère qu'en pratique, les difficultés sont plus importantes.
J'ai le fichier Xml suivant :
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
|
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE boost_serialization>
<boost_serialization signature="serialization::archive" version="5">
<itineraire>
<nbCheckpoint>12</nbCheckpoint>
<checkpoint>
<pointX>2296.0</pointX>
<pointY>3476.0</pointY>
</checkpoint>
<checkpoint>
<pointX>2356.0</pointX>
<pointY>3212.0</pointY>
</checkpoint>
<checkpoint>
<pointX>2008.0</pointX>
<pointY>2804.0</pointY>
</checkpoint>
<checkpoint>
<pointX>1708.0</pointX>
<pointY>2624.0</pointY>
</checkpoint>
<checkpoint>
<pointX>1332.0</pointX>
<pointY>2780.0</pointY>
</checkpoint>
<checkpoint>
<pointX>1444.0</pointX>
<pointY>2440.0</pointY>
</checkpoint>
<checkpoint>
<pointX>1444.0</pointX>
<pointY>2160.0</pointY>
</checkpoint>
<checkpoint>
<pointX>1804.0</pointX>
<pointY>2212.0</pointY>
</checkpoint>
<checkpoint>
<pointX>2336.0</pointX>
<pointY>2348.0</pointY>
</checkpoint>
<checkpoint>
<pointX>2160.0</pointX>
<pointY>1940.0</pointY>
</checkpoint>
<checkpoint>
<pointX>2064.0</pointX>
<pointY>840.0</pointY>
</checkpoint>
<checkpoint>
<pointX>2396.0</pointX>
<pointY>688.0</pointY>
</checkpoint>
</itineraire>
</boost_serialization> |
Je souhaite donc extraire de ce fichier un itinéraire. Voici comment s'articulent les classes sensées extraire un Itinéraire de mon fichier XML :
La classe Itinéraire (le .h) :
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
|
#ifndef ITINERAIRE_H
#define ITINERAIRE_H
#include "PointItineraire.h"
class Itineraire {
private :
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive& ar, const unsigned int version)
{
ar & BOOST_SERIALIZATION_NVP(checkpoint);
}
public :
std::vector<PointItineraire> checkpoint;
// Constructeur/destructeur
/**
* Constructeur de la classe
*/
Itineraire();
/**
* Destructeur de la classe
*/
~Itineraire();
};
#endif |
Ma classe PointItinéraire (le .h) :
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
|
#ifndef POINTITINERAIRE_H
#define POINTITINERAIRE_H
// archives Boost
#include <boost/archive/xml_oarchive.hpp>
// archives Boost
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
// pour la sérialisation de std::vector
#include <boost/serialization/vector.hpp>
class PointItineraire {
public :
double pointX;
double pointY;
// Constructeur/destructeur
/**
* Constructeur de la classe
*/
PointItineraire();
/**
* Destructeur de la classe
*/
~PointItineraire();
private :
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive& ar, const unsigned int version)
{
ar & BOOST_SERIALIZATION_NVP(pointX) &
BOOST_SERIALIZATION_NVP(pointY);
}
};
#endif |
Et pour finir, la classe qui appelle la génération :
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
|
// le .h
#ifndef ITINERAIREXMLPARSING_H
#define ITINERAIREXMLPARSING_H
#include "Itineraire.h"
class ItineraireXMLParsing {
private :
std::string m_sadresseFichierAParser;
Itineraire itineraire;
public :
// Constructeur/destructeur
/**
* Constructeur de la classe
*/
ItineraireXMLParsing(std::string adresseFichierAParser);
/**
* Destructeur de la classe
*/
~ItineraireXMLParsing();
void extraireInformationFichier();
};
#endif
// le .c
#include "ItineraireXMLParsing.h"
#include <fstream>
#include <sstream>
// Constructeur/destructeur
/**
* Constructeur de la classe
*/
ItineraireXMLParsing::ItineraireXMLParsing(std::string adresseFichierAParser)
{
m_sadresseFichierAParser = adresseFichierAParser;
}
/**
* Destructeur de la classe
*/
ItineraireXMLParsing::~ItineraireXMLParsing(){}
void ItineraireXMLParsing::extraireInformationFichier()
{
//récupération des infos Itineraire
//constructeur de ifile avec un char* en paramètre
std::ifstream ifile(m_sadresseFichierAParser.c_str());
boost::archive::xml_iarchive ia(ifile);
ia >> BOOST_SERIALIZATION_NVP(itineraire);
} |
Mais rien ne se passe après "ia >> BOOST_SERIALIZATION_NVP(itineraire);". Le problème vient certainement du fait que mon objet itinéraire contient en attribut un vector de pointItinéraire, et qu'il ne sait pas comment récupérer les informations de ces points dans le xml. Mais je ne sais pas comment lui indiquer cela, les tutoriels sont simplistes, et axés sur l'extraction de données de bases.