views:

615

answers:

3

I'm searching for a tutorial to load a XML file, read it, change it and finally save it with C++. I'm using Linux Ubuntu and tried to use Xerces. With Google and much time, I could only load an XML File:

#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;
}

How do I can read an manipulate the XML file and finally save it? Is there alternative libraries? (I tried tinyxml but the files returns errors, when I want to compile it)

+1  A: 

LibXML++ appears to be the best one for C++. Feature-wise it is very complete, including XPath, charset conversions (by Glibmm) and everything that you'd expect in an XML library. It uses traditional DOM and SAX APIs, which counts as a pro or a con depending on whom you ask from. One possible issue is that the dependencies of the library are extremely heavy (due to the use of Glibmm). Still, it appears to be the only decent XML library for C++.

http://libxmlplusplus.sourceforge.net/docs/manual/html/index.html

TinyXML does not parse XML according to the specification, so I would recommend against it, even though it works for simple documents.

Tronic
A: 

The sample CreateDOMDocument that comes with Xerces shows you how to add nodes etc. to a DOM document. The code you have so far creates the document, so you need to adapt the code in the second sample to add nodes, attributes etc.

Also, note that when you say:

 cout << docRootNode->getAttributes() << endl; 

the getAttributes function returns a collection of attributes - you need to apply further Xerces functions to that collection to get the contained information.

Note that if you want to extract a subset of the data in in e XML file, it may be easier to use an event driven SAX parser (Xerces includes one of these) rather than building and walking a complete DOM document.

anon
A: 

If you want to see an example of how to do that using Xerces-C++, check out this code:

http://libprf1.tigris.org/files/documents/1338/13256/libprf1-0.1R3.tar.gz

I wrote it a long time ago as a university project. It's most likely based on an outdated version of Xerces-C++, but I don't think the API has changed too much to be a problem. It will at least give you an idea.

deemoowoor