Sto cercando un tutorial per caricare un file XML, leggerlo, cambiarlo e infine salvarlo con C++. Sto usando Linux Ubuntu e ho provato a usare Xerces. Con Google e molto tempo, ho potuto caricare solo un file XML:Xerces C++ - Carica, leggi e salva, alternative?
#include <xercesc/parsers/XercesDOMParser.hpp>
#include <xercesc/dom/DOM.hpp>
#include <xercesc/sax/HandlerBase.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/util/PlatformUtils.hpp>
#include <iostream>
using namespace std;
using namespace xercesc;
int main (int argc, char* args[]) {
try {
XMLPlatformUtils::Initialize();
}
catch (const XMLException& toCatch) {
char* message = XMLString::transcode(toCatch.getMessage());
cout << "Error during initialization! :\n"
<< message << "\n";
XMLString::release(&message);
return 1;
}
XercesDOMParser* parser = new XercesDOMParser();
parser->setValidationScheme(XercesDOMParser::Val_Always);
parser->setDoNamespaces(true); // optional
ErrorHandler* errHandler = (ErrorHandler*) new HandlerBase();
parser->setErrorHandler(errHandler);
const char* xmlFile = "demo.xml";
try {
parser->parse(xmlFile);
}
catch (const XMLException& toCatch) {
char* message = XMLString::transcode(toCatch.getMessage());
cout << "Exception message is: \n"
<< message << "\n";
XMLString::release(&message);
return -1;
}
catch (const DOMException& toCatch) {
char* message = XMLString::transcode(toCatch.msg);
cout << "Exception message is: \n"
<< message << "\n";
XMLString::release(&message);
return -1;
}
catch (...) {
cout << "Unexpected Exception \n" ;
return -1;
}
DOMNode* docRootNode;
// DOMNode* aNode;
DOMDocument* doc;
doc = parser->getDocument();
docRootNode = doc->getDocumentElement();
cout << docRootNode->getAttributes() << endl; //returns Hex
delete parser;
delete errHandler;
return 0;
}
Come si può leggere una manipolare i file XML e infine salvarlo? Esistono librerie alternative? (Ho provato tinyxml ma i file restituiscono errori, quando voglio compilarlo)
Spero che tu usi il nuovo Xerces 3. * il vecchio 2.7/2.8 sta avendo perdite di memoria dappertutto –