I want to parse simple XML file using xerces, for that I download ‘xerces-c-3.1.4’. From their sample code I did the DomPrint program in visual studio 2013.
here is my code:
#include <xercesc/util/PlatformUtils.hpp>
#include "xercesc/util/PlatformUtils.hpp"
#include "D:\xecerces_download\xerces-c-3.1.4\src\xercesc\util\PlatformUtils.hpp"
#include"D:\xecerces_download\xerces-c-3.1.4\src\xercesc\framework\StdOutFormatTarget.cpp"
#include"D:\xecerces_download\xerces-c-3.1.4\src\xercesc\framework\LocalFileFormatTarget.cpp"
#include <xercesc/dom/DOM.hpp>
#include <xercesc/framework/StdOutFormatTarget.hpp>
#include <xercesc/framework/LocalFileFormatTarget.hpp>
#include <xercesc/parsers/XercesDOMParser.hpp>
#include <xercesc/util/XMLUni.hpp>
#include "DOMTreeErrorReporter.hpp"
#include "DOMPrintFilter.hpp"
#include "DOMPrintErrorHandler.hpp"
#include <xercesc/util/OutOfMemoryException.hpp>
#include <string.h>
#include <stdlib.h>
// ---------------------------------------------------------------------------
static char* gXmlFile = 0;
static bool gDoNamespaces = false;
static bool gDoSchema = false;
static bool gSchemaFullChecking = false;
static bool gDoCreate = false;
static char* goutputfile = 0;
static char* gXPathExpression = 0;
// options for DOMLSSerializer's features
static XMLCh* gOutputEncoding = 0;
static bool gSplitCdataSections = true;
static bool gDiscardDefaultContent = true;
static bool gUseFilter = false;
static bool gFormatPrettyPrint = false;
static bool gWriteBOM = false;
static XercesDOMParser::ValSchemes gValScheme = XercesDOMParser::Val_Auto;
// Prototypes for internally used functions
void usage();
// ---------------------------------------------------------------------------
//
// Usage()
//
// ---------------------------------------------------------------------------
void usage()
{
XERCES_STD_QUALIFIER cout << "
Usage:
"
" DOMPrint [options] <XML file>
"
"This program invokes the DOM parser, and builds the DOM tree.
"
"It then asks the DOMLSSerializer to serialize the DOM tree.
"
"Options:
"
" -e create entity reference nodes. Default is no expansion.
"
" -v=xxx Validation scheme [always | never | auto*].
"
" -n Enable namespace processing. Default is off.
"
" -s Enable schema processing. Default is off.
"
" -f Enable full schema constraint checking. Defaults is off.
"
" -wenc=XXX Use a particular encoding for output. Default is
"
" the same encoding as the input XML file. UTF-8 if
"
" input XML file has not XML declaration.
"
" -wfile=xxx Write to a file instead of stdout.
"
" -wscs=xxx Enable/Disable split-cdata-sections. Default on
"
" -wddc=xxx Enable/Disable discard-default-content. Default on
"
" -wflt=xxx Enable/Disable filtering. Default off
"
" -wfpp=xxx Enable/Disable format-pretty-print. Default off
"
" -wbom=xxx Enable/Disable write Byte-Order-Mark Default off
"
" -xpath=xxx Prints only the nodes matching the given XPath.
"
" -? Show this help.
"
" * = Default if not provided explicitly.
"
"The parser has intrinsic support for the following encodings:
"
" UTF-8, US-ASCII, ISO8859-1, UTF-16[BL]E, UCS-4[BL]E,
"
" WINDOWS-1252, IBM1140, IBM037, IBM1047.
"
<< XERCES_STD_QUALIFIER endl;
}
// ---------------------------------------------------------------------------
//
// main
//
// ---------------------------------------------------------------------------
int main(int argC, char* argV[])
{
int retval = 0;
// Initialize the XML4C2 system
try
{
XMLPlatformUtils::Initialize();
}
catch (const XMLException &toCatch)
{
XERCES_STD_QUALIFIER cerr << "Error during Xerces-c Initialization.
"
<< " Exception message:"
<< StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl;
return 1;
}
// Check command line and extract arguments.
if (argC < 2)
{
usage();
XMLPlatformUtils::Terminate();
return 1;
}
// See if non validating dom parser configuration is requested.
int parmInd;
for (parmInd = 1; parmInd < argC; parmInd++)
{
// Break out on first parm not starting with a dash
if (argV[parmInd][0] != '-')
break;
// Watch for special case help request
if (!strcmp(argV[parmInd], "-?"))
{
usage();
XMLPlatformUtils::Terminate();
return 2;
}
else if (!strncmp(argV[parmInd], "-v=", 3)
|| !strncmp(argV[parmInd], "-V=", 3))
{
const char* const parm = &argV[parmInd][3];
if (!strcmp(parm, "never"))
gValScheme = XercesDOMParser::Val_Never;
else if (!strcmp(parm, "auto"))
gValScheme = XercesDOMParser::Val_Auto;
else if (!strcmp(parm, "always"))
gValScheme = XercesDOMParser::Val_Always;
else
{
XERCES_STD_QUALIFIER cerr << "Unknown -v= value: " << parm << XERCES_STD_QUALIFIER endl;
XMLPlatformUtils::Terminate();
return 2;
}
}
else if (!strcmp(argV[parmInd], "-n")
|| !strcmp(argV[parmInd], "-N"))
{
gDoNamespaces = true;
}
else if (!strcmp(argV[parmInd], "-s")
|| !strcmp(argV[parmInd], "-S"))
{
gDoSchema = true;
}
else if (!strcmp(argV[parmInd], "-f")
|| !strcmp(argV[parmInd], "-F"))
{
gSchemaFullChecking = true;
}
else if (!strcmp(argV[parmInd], "-e")
|| !strcmp(argV[parmInd], "-E"))
{
gDoCreate = true;
}
else if (!strncmp(argV[parmInd], "-wenc=", 6))
{
// Get out the encoding name
gOutputEncoding = XMLString::transcode(&(argV[parmInd][6]));
}
else if (!strncmp(argV[parmInd], "-wfile=", 7))
{
goutputfile = &(argV[parmInd][7]);
}
else if (!strncmp(argV[parmInd], "-wddc=", 6))
{
const char* const parm = &argV[parmInd][6];
if (!strcmp(parm, "on"))
gDiscardDefaultContent = true;
else if (!strcmp(parm, "off"))
gDiscardDefaultContent = false;
else
{
XERCES_STD_QUALIFIER cerr << "Unknown -wddc= value: " << parm << XERCES_STD_QUALIFIER endl;
XMLPlatformUtils::Terminate();
return 2;
}
}
else if (!strncmp(argV[parmInd], "-wscs=", 6))
{
const char* const parm = &argV[parmInd][6];
if (!strcmp(parm, "on"))
gSplitCdataSections = true;
else if (!strcmp(parm, "off"))
gSplitCdataSections = false;
else
{
XERCES_STD_QUALIFIER cerr << "Unknown -wscs= value: " << parm << XERCES_STD_QUALIFIER endl;
XMLPlatformUtils::Terminate();
return 2;
}
}
else if (!strncmp(argV[parmInd], "-wflt=", 6))
{
const char* const parm = &argV[parmInd][6];
if (!strcmp(parm, "on"))
gUseFilter = true;
else if (!strcmp(parm, "off"))
gUseFilter = false;
else
{
XERCES_STD_QUALIFIER cerr << "Unknown -wflt= value: " << parm << XERCES_STD_QUALIFIER endl;
XMLPlatformUtils::Terminate();
return 2;
}
}
else if (!strncmp(argV[parmInd], "-wfpp=", 6))
{
const char* const parm = &argV[parmInd][6];
if (!strcmp(parm, "on"))
gFormatPrettyPrint = true;
else if (!strcmp(parm, "off"))
gFormatPrettyPrint = false;
else
{
XERCES_STD_QUALIFIER cerr << "Unknown -wfpp= value: " << parm << XERCES_STD_QUALIFIER endl;
XMLPlatformUtils::Terminate();
return 2;
}
}
else if (!strncmp(argV[parmInd], "-wbom=", 6))
{
const char* const parm = &argV[parmInd][6];
if (!strcmp(parm, "on"))
gWriteBOM = true;
else if (!strcmp(parm, "off"))
gWriteBOM = false;
else
{
XERCES_STD_QUALIFIER cerr << "Unknown -wbom= value: " << parm << XERCES_STD_QUALIFIER endl;
XMLPlatformUtils::Terminate();
return 2;
}
}
else if (!strncmp(argV[parmInd], "-xpath=", 7))
{
gXPathExpression = &(argV[parmInd][7]);
}
else
{
XERCES_STD_QUALIFIER cerr << "Unknown option '" << argV[parmInd]
<< "', ignoring it.
" << XERCES_STD_QUALIFIER endl;
}
}
if (parmInd + 1 != argC)
{
usage();
XMLPlatformUtils::Terminate();
return 1;
}
gXmlFile = argV[parmInd];
//
XercesDOMParser *parser = new XercesDOMParser;
parser->setValidationScheme(gValScheme);
parser->setDoNamespaces(gDoNamespaces);
parser->setDoSchema(gDoSchema);
parser->setHandleMultipleImports(true);
parser->setValidationSchemaFullChecking(gSchemaFullChecking);
parser->setCreateEntityReferenceNodes(gDoCreate);
DOMTreeErrorReporter *errReporter = new DOMTreeErrorReporter();
parser->setErrorHandler(errReporter);
//
bool errorsOccured = false;
try
{
parser->parse(gXmlFile);
}
catch (const OutOfMemoryException&)
{
XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
errorsOccured = true;
}
catch (const XMLException& e)
{
XERCES_STD_QUALIFIER cerr << "An error occurred during parsing
Message: "
<< StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
errorsOccured = true;
}
catch (const DOMException& e)
{
const unsigned int maxChars = 2047;
XMLCh errText[maxChars + 1];
XERCES_STD_QUALIFIER cerr << "
DOM Error during parsing: '" << gXmlFile << "'
"
<< "DOMException code is: " << e.code << XERCES_STD_QUALIFIER endl;
if (DOMImplementation::loadDOMExceptionMsg(e.code, errText, maxChars))
XERCES_STD_QUALIFIER cerr << "Message is: " << StrX(errText) << XERCES_STD_QUALIFIER endl;
errorsOccured = true;
}
catch (...)
{
XERCES_STD_QUALIFIER cerr << "An error occurred during parsing
" << XERCES_STD_QUALIFIER endl;
errorsOccured = true;
}
// If the parse was successful, output the document data from the DOM tree
if (!errorsOccured && !errReporter->getSawErrors())
{
DOMPrintFilter *myFilter = 0;
try
{
// get a serializer, an instance of DOMLSSerializer
XMLCh tempStr[3] = { chLatin_L, chLatin_S, chNull };
DOMImplementation *impl = DOMImplementationRegistry::getDOMImplementation(tempStr);
DOMLSSerializer *theSerializer = ((DOMImplementationLS*)impl)->createLSSerializer();
DOMLSOutput *theOutputDesc = ((DOMImplementationLS*)impl)->createLSOutput();
// set user specified output encoding
theOutputDesc->setEncoding(gOutputEncoding);
// plug in user's own filter
if (gUseFilter)
{
myFilter = new DOMPrintFilter(DOMNodeFilter::SHOW_ELEMENT |
DOMNodeFilter::SHOW_ATTRIBUTE |
DOMNodeFilter::SHOW_DOCUMENT_TYPE);
theSerializer->setFilter(myFilter);
}
// plug in user's own error handler
DOMErrorHandler *myErrorHandler = new DOMPrintErrorHandler();
DOMConfiguration* serializerConfig = theSerializer->getDomConfig();
serializerConfig->setParameter(XMLUni::fgDOMErrorHandler, myErrorHandler);
// set feature if the serializer supports the feature/mode
if (serializerConfig->canSetParameter(XMLUni::fgDOMWRTSplitCdataSections, gSplitCdataSections))
serializerConfig->setParameter(XMLUni::fgDOMWRTSplitCdataSections, gSplitCdataSections);
if (serializerConfig->canSetParameter(XMLUni::fgDOMWRTDiscardDefaultContent, gDiscardDefaultContent))
serializerConfig->setParameter(XMLUni::fgDOMWRTDiscardDefaultContent, gDiscardDefaultContent);
if (serializerConfig->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, gFormatPrettyPrint))
serializerConfig->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, gFormatPrettyPrint);
if (serializerConfig->canSetParameter(XMLUni::fgDOMWRTBOM, gWriteBOM))
serializerConfig->setParameter(XMLUni::fgDOMWRTBOM, gWriteBOM);
XMLFormatTarget *myFormTarget;
if (goutputfile)
myFormTarget = new LocalFileFormatTarget(goutputfile);
else
myFormTarget = new StdOutFormatTarget();
theOutputDesc->setByteStream(myFormTarget);
// get the DOM representation
DOMDocument *doc = parser->getDocument();
//
// do the serialization through DOMLSSerializer::write();
//
if (gXPathExpression != NULL)
{
XMLCh* xpathStr = XMLString::transcode(gXPathExpression);
DOMElement* root = doc->getDocumentElement();
try
{
DOMXPathNSResolver* resolver = doc->createNSResolver(root);
DOMXPathResult* result = doc->evaluate(
xpathStr,
root,
resolver,
DOMXPathResult::ORDERED_NODE_SNAPSHOT_TYPE,
NULL);
XMLSize_t nLength = result->getSnapshotLength();
for (XMLSize_t i = 0; i < nLength; i++)
{
result->snapshotItem(i);
theSerializer->write(result->getNodeValue(), theOutputDesc);
}
result->release();
resolver->release();
}
catch (const DOMXPathException& e)
{
XERCES_STD_QUALIFIER cerr << "An error occurred during processing of the XPath expression. Msg is:"
<< XERCES_STD_QUALIFIER endl
<< StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
retval = 4;
}
catch (const DOMException& e)
{
XERCES_STD_QUALIFIER cerr << "An error occurred during processing of the XPath expression. Msg is:"
<< XERCES_STD_QUALIFIER endl
<< StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
retval = 4;
}
XMLString::release(&xpathStr);
}
else
theSerializer->write(doc, theOutputDesc);
theOutputDesc->release();
theSerializer->release();
delete myFormTarget;
delete myErrorHandler;
if (gUseFilter)
delete myFilter;
}
catch (const OutOfMemoryException&)
{
XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
retval = 5;
}
catch (XMLException& e)
{
XERCES_STD_QUALIFIER cerr << "An error occurred during creation of output transcoder. Msg is:"
<< XERCES_STD_QUALIFIER endl
<< StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
retval = 4;
}
}
else
retval = 4;
delete errReporter;
delete parser;
XMLString::release(&gOutputEncoding);
XMLPlatformUtils::Terminate();
return retval;
}
which showing errors: 1. Cannot open include file: ‘xercesc/util/PlatformUtils.hpp’: No such file or directory
2. IntelliSense: ‘operator=’ must be a member function
3. IntelliSense: cannot open source file “xercesc/util/PanicHandler.hpp”
4. IntelliSense: cannot open source file “xercesc/util/XercesDefs.hpp” and many more.
for that 1.I added respective .hpp file 2. set precompiled header–>no 3. set path " D:\xecerces_download\xerces-c-3.1.4\src\xercesc" in project–>property–>c/c+±->general 4. project–>property–>linker–>input–>xerces-c_3D.lib;%(AdditionalDependencies)
still so many errors are there,Can you please help me to sort it out?