Thanks a lot Alberto. It's working fine now. :) -----Original Message----- From: ext Alberto Massari [mailto:[EMAIL PROTECTED] Sent: Wednesday, January 30, 2008 7:42 PM To: [email protected] Subject: Re: Problems in parsing; values not printed on the screen.
Kanagal, Rahul (NSN - IN/India - MiniMD) wrote: > Hello, > > The number of elements message is not printed, and there are no > non-ASCII characters. > This means that your "if(doc)" test fails; and by looking at your code again, it's clear that you never initialize it. Replace parser->parse(*wrapper); with doc = parser->parse(*wrapper); Alberto > -----Original Message----- > From: ext Alberto Massari [mailto:[EMAIL PROTECTED] > Sent: Wednesday, January 30, 2008 5:10 PM > To: [email protected] > Subject: Re: Problems in parsing; values not printed on the screen. > > It's hard to say just by looking at this fragment of code. Do you see > the total "number of elements=XX" message? How many elements did the > code find? Does your fragment contain non-ASCII characters that > XMLString::transcode could fail to translate? > > Alberto > > Kanagal, Rahul (NSN - IN/India - MiniMD) wrote: > >> Hello, >> >> I am trying to write a decode function(using DOM parser) to decode >> XML >> > > >> content in a buffer and print the values of the element names,values >> and their attributes. >> >> The code is just a modification of the sample code from apache which >> parses XML content from a file. The code works just fine for file >> parse, but the modified code for parsing buffer contents is just not >> printing the values on the screen. I am a new developer and I am not >> able to find out what the problem is.Can somebody please help? >> >> Thanks, >> Rahul. >> >> Here is a sample of the code: >> >> >> // >> --------------------------------------------------------------------- >> - >> ----- >> >> // Includes >> // >> --------------------------------------------------------------------- >> - >> ----- >> >> #include <xercesc/util/PlatformUtils.hpp> #include >> <xercesc/parsers/AbstractDOMParser.hpp> >> #include <xercesc/dom/DOMImplementation.hpp> >> #include <xercesc/dom/DOMImplementationLS.hpp> >> #include <xercesc/dom/DOMImplementationRegistry.hpp> >> #include <xercesc/dom/DOMBuilder.hpp> #include >> <xercesc/dom/DOMException.hpp> #include <xercesc/dom/DOMDocument.hpp> >> #include <xercesc/dom/DOMNodeList.hpp> #include >> <xercesc/dom/DOMError.hpp> #include <xercesc/dom/DOMLocator.hpp> >> #include <xercesc/dom/DOMNamedNodeMap.hpp> #include >> <xercesc/dom/DOMAttr.hpp> >> >> #include <xercesc/framework/Wrapper4DOMInputSource.hpp> >> #include <xercesc/framework/Wrapper4InputSource.hpp> >> #include <xercesc/framework/MemBufInputSource.hpp> >> >> >> #include "DOMCount.h" >> #include <string.h> >> #include <stdlib.h> >> #include <iostream> >> #if defined(XERCES_NEW_IOSTREAMS) >> #include <fstream> >> #else >> #include <fstream.h> >> #endif >> #include"DOMDecode.hpp" >> using namespace std; >> // >> --------------------------------------------------------------------- >> - >> ----- >> >> // This is a simple program which invokes the DOMParser to build a >> DOM // tree for the specified input file. It then walks the tree and >> prints out the // elements and their values. >> // >> --------------------------------------------------------------------- >> - >> ----- >> >> >> static int decodeChildElements(DOMNode *n) { >> DOMNode *child; >> int count = 0; >> if (n) { >> if (n->getNodeType() == DOMNode::ELEMENT_NODE) >> { >> char *name = >> XMLString::transcode(n->getNodeName()); >> XERCES_STD_QUALIFIER cout <<"Element Name : "<< name >> << XERCES_STD_QUALIFIER endl; >> >> >> name = >> XMLString::transcode(n->getFirstChild()->getNodeValue()); >> XERCES_STD_QUALIFIER cout <<"Element Value : "<< name >> << XERCES_STD_QUALIFIER endl; >> >> >> >> XMLString::release(&name); >> >> if(n->hasAttributes()) { >> // get all the attributes of the node >> DOMNamedNodeMap *pAttributes = n->getAttributes(); >> int nSize = pAttributes->getLength(); >> XERCES_STD_QUALIFIER cout <<"\tAttributes" << >> XERCES_STD_QUALIFIER endl; >> XERCES_STD_QUALIFIER cout <<"\t----------" << >> XERCES_STD_QUALIFIER endl; >> for(int i=0;i<nSize;++i) { >> DOMAttr *pAttributeNode = (DOMAttr*) >> pAttributes->item(i); >> // get attribute name >> char *name = >> XMLString::transcode(pAttributeNode->getName()); >> >> XERCES_STD_QUALIFIER cout << "\t" << name << >> > "="; > >> XMLString::release(&name); >> >> // get attribute type >> name = >> XMLString::transcode(pAttributeNode->getValue()); >> XERCES_STD_QUALIFIER cout << name << >> XERCES_STD_QUALIFIER endl; >> XMLString::release(&name); >> } >> } >> ++count; >> } >> for (child = n->getFirstChild(); child != 0; >> child=child->getNextSibling()) >> count += decodeChildElements(child); >> } >> return count; >> } >> >> >> int decodeXml(const char *buf) >> { >> static const char* bufId = "XML content"; >> >> AbstractDOMParser::ValSchemes valScheme = >> AbstractDOMParser::Val_Auto; >> bool doNamespaces = false; >> bool doSchema = false; >> bool schemaFullChecking = false; >> bool doList = false; >> bool errorOccurred = false; >> bool recognizeNEL = false; >> char localeStr[64]; >> memset(localeStr, 0, sizeof localeStr); >> >> // Initialize the XML4C system >> try >> { >> if (strlen(localeStr)) >> { >> XMLPlatformUtils::Initialize(localeStr); >> } >> else >> { >> XMLPlatformUtils::Initialize(); >> } >> >> if (recognizeNEL) >> { >> XMLPlatformUtils::recognizeNEL(recognizeNEL); >> } >> } >> >> catch (const XMLException& toCatch) >> { >> XERCES_STD_QUALIFIER cerr << "Error during initialization! >> > :\n" > >> << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER >> > endl; > >> return 1; >> } >> >> // Instantiate the DOM parser. >> static const XMLCh gLS[] = { chLatin_L, chLatin_S, chNull }; >> DOMImplementation *impl = >> DOMImplementationRegistry::getDOMImplementation(gLS); >> DOMBuilder *parser = >> >> > ((DOMImplementationLS*)impl)->createDOMBuilder(DOMImplementationLS::MO > DE > _SYNCHRONOUS, > >> 0); >> >> parser->setFeature(XMLUni::fgDOMNamespaces, doNamespaces); >> parser->setFeature(XMLUni::fgXercesSchema, doSchema); >> parser->setFeature(XMLUni::fgXercesSchemaFullChecking, >> schemaFullChecking); >> >> if (valScheme == AbstractDOMParser::Val_Auto) >> { >> parser->setFeature(XMLUni::fgDOMValidateIfSchema, true); >> } >> else if (valScheme == AbstractDOMParser::Val_Never) >> { >> parser->setFeature(XMLUni::fgDOMValidation, false); >> } >> else if (valScheme == AbstractDOMParser::Val_Always) >> { >> parser->setFeature(XMLUni::fgDOMValidation, true); >> } >> >> // enable datatype normalization - default is off >> parser->setFeature(XMLUni::fgDOMDatatypeNormalization, true); >> >> // And create our error handler and install it >> DOMCountErrorHandler errorHandler; >> parser->setErrorHandler(&errorHandler); >> >> bool more = true; >> XERCES_STD_QUALIFIER ifstream fin; >> >> //......creating a MemBufInputSource >> * MemBufInputSource* memBufIS = new MemBufInputSource((const >> XMLByte*)buf, strlen(buf), bufId, false);* >> * Wrapper4InputSource *wrapper=new >> Wrapper4InputSource(memBufIS,false);* >> >> while (more) >> { >> more = false; >> >> //reset error count first >> errorHandler.resetErrors(); >> >> XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *doc = 0; >> >> try >> { >> // reset document pool >> parser->resetDocumentPool(); >> * parser->parse(*wrapper);* >> >> } >> >> catch (const XMLException& toCatch) >> { >> XERCES_STD_QUALIFIER cerr << "\nError during parsing: '" >> << buf << "'\n" >> << "Exception message is: \n" >> << StrX(toCatch.getMessage()) << "\n" << >> XERCES_STD_QUALIFIER endl; >> errorOccurred = true; >> continue; >> } >> catch (const DOMException& toCatch) >> { >> const unsigned int maxChars = 2047; >> XMLCh errText[maxChars + 1]; >> >> XERCES_STD_QUALIFIER cerr << "\nDOM Error during parsing: >> '" << buf<< "'\n" >> << "DOMException code is: " << toCatch.code << >> XERCES_STD_QUALIFIER endl; >> >> if (DOMImplementation::loadDOMExceptionMsg(toCatch.code, >> errText, maxChars)) >> XERCES_STD_QUALIFIER cerr << "Message is: " << >> StrX(errText) << XERCES_STD_QUALIFIER endl; >> >> errorOccurred = true; >> continue; >> } >> catch (...) >> { >> XERCES_STD_QUALIFIER cerr << "\nUnexpected exception >> during parsing: '" << buf << "'\n"; >> errorOccurred = true; >> continue; >> } >> >> if (errorHandler.getSawErrors()) >> { >> XERCES_STD_QUALIFIER cout << "\nErrors occurred, no >> output >> > > >> available\n" << XERCES_STD_QUALIFIER endl; >> errorOccurred = true; >> } >> else >> { >> if (doc) { >> >> int elementCount = >> decodeChildElements((DOMNode*)doc->getDocumentElement()); >> cout<<"total number of >> > elements="<<elementCount<<endl; > >> >> } >> >> } >> >> } >> >> // >> // Delete the parser itself. Must be done prior to calling >> Terminate, below. >> // >> parser->release(); >> >> // And call the termination method >> XMLPlatformUtils::Terminate(); >> >> if (doList) >> fin.close(); >> >> if (errorOccurred) >> return 4; >> else >> return 0; >> } >> >> >> >> > > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > > > --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
