Tuesday, May 21, 2013

How to use Xerces Library to parse the XML file with Visual C++ Project

10:20 AM



The Apache project's Xerces-C libraries support the DOM approach to XML parsing. The entire XML file is imported into memory and the data is held as nodes in a data tree which can be traversed for information.
The Xerces-C C++ parser home page: http://xml.apache.org/xerces-c/


So download the latest version of  Xerces Library and place it into your project  folder.
The Xerces Library folder contains the following files and folders inside it Shown in the picture .



Now make a new project or open your current project and Right click your project and go to “project properties”. Now go to “VC++ directories” and then go to “Include directories” and give the path of the Xerces Library include folder as shown in the picture below.



Now go to “VC++ directories” and then go to “Library directories” and give the path of the Xerces Library lib folder as shown in the picture below.



Now go to “Linker” -> “Input” and then go to “Additional directories” and give the path of the Xerces Library file as shown in the picture below.

Now write the following code to parse the directories.



#include <iostream>
#include <xercesc/dom/DOM.hpp>
#include <xercesc/dom/DOMElement.hpp>
#include <xercesc/dom/DOMNode.hpp>
#include <xercesc/dom/DOMText.hpp>
#include <xercesc/parsers/XercesDOMParser.hpp>
#include <xercesc/sax/HandlerBase.hpp>

using namespace std;
using namespace xercesc;

void write(DOMNode* node);
void writeElement(DOMElement* element);
void writeText(DOMText* text);

void
write(DOMNode* node) {
     if (node) {
           switch (node->getNodeType()) {
           case DOMNode::ELEMENT_NODE:
                writeElement(static_cast<DOMElement*>(node));
                break;
           case DOMNode::TEXT_NODE:
                writeText(static_cast<DOMText*>(node));
                break;
           }

           DOMNode* child = node->getFirstChild();
           while (child) {
                DOMNode* next = child->getNextSibling();
                write(child);
                child = next;
           }
     }
}

void
writeElement(DOMElement* element) {
     char* name = XMLString::transcode(element->getTagName());
     cout << "tag    : " << name << endl;
     XMLString::release(&name);

     DOMNamedNodeMap* map = element->getAttributes();
     for (XMLSize_t i = 0; i < map->getLength(); i++) {
           DOMAttr* attr = static_cast<DOMAttr*>(map->item(i));
           char* attr_name  = XMLString::transcode(attr->getName());
           char* attr_value = XMLString::transcode(attr->getValue());
           cout << attr_name << ": "<< attr_value << endl;

           XMLString::release(&attr_name);
           XMLString::release(&attr_value);
     }
}

void
writeText(DOMText* text) {
     XMLCh* buffer = new XMLCh[XMLString::stringLen(text->getData()) + 1];
     XMLString::copyString(buffer, text->getData());
     XMLString::trim(buffer);
     char* content=XMLString::transcode(buffer);
     delete[] buffer;

     cout << "content: " << content << endl;
     XMLString::release(&content);
}

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 = "samples/data/personal.xml";

     try {
           parser->parse(xmlFile);
           DOMDocument* dom = parser->getDocument();
           write(dom);
     } 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;
     }

     delete parser;
     delete errHandler;
     return 0;
}






Compile:
RPM installed: g++ -g -Wall -pedantic -lxerces-c parser.cpp -DMAIN_TEST -o parser 

or
Installed to "/opt": g++ -g -Wall -pedantic -I/opt/include -L/opt/lib -lxerces-c parser.cpp -DMAIN_TEST -o parser



Now Run  the program..


































































-----------------------------------------------------


Searches related to xerces c++ example
xerces xpath example c++
xerces c++ tutorial
xerces c++ sax parser example
xerces samples
libxerces
Xerces for C++ Using Visual C++
Searches related to xerces visual c++ example
visual studio example
visual basic example
C++ XML with Xerces
Configuring Xerces-C++ XML Libraries with Visual Studio 2008
c++ parse xml
c++ read xml
Xerces for C++ Tutorial Using Visual C++
xml reader c++
Linux Tutorial: Parsing XML with Xerces-C C++ API
Xerces-C++ Samples
Xerces for C++ Tutorial Using Visual C++ 
Xerces for C++







Written by

We are one of the initiators of the development of information technology in understanding the need for a solution that is familiar and close to us.

0 comments:

Post a Comment

 

© 2013 Klick Dev. All rights resevered.

Back To Top