You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xerces.apache.org by "Coker, Jonathan M" <jo...@boeing.com> on 2007/05/09 22:01:03 UTC

UTF error with SAX

Hello,
   I am trying to set up a program that will read data via TCP/IP then
save it in a buffer for parsing.  I am using SAX (first time with it)
and I am getting a UTFDataFormatException.  I am including the code for
doing this and the error I am seeing.  Any comments/suggestions on my
method or implementation would be appreciated.

//called from main()
int doIt()
{     
     static const char* sourceName = "inputSource";
     XMLByte* xmlBuffer;
     const char* xmlString = "<a></a>";  
     try{
          XMLPlatformUtils::Initialize();
     }
     catch (const XMLException& toCatch)
     {
          string message(XMLString::transcode(toCatch.getMessage()));
          cout<<"Error during initialization. "<<endl;
          cout<<"Message is "<<message<<endl;;
          return 1;
     }
     
     xmlBuffer =  new XMLByte[strlen(xmlString)];
     memcpy(xmlBuffer,xmlString, strlen(xmlString));
     
     MemBufInputSource* inputSource =   new
MemBufInputSource(xmlBuffer,512,sourceName);  
  
     SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();   
     
     DefaultHandler* pduHandler;
     DefaultHandler* pduErrors;

     pduHandler = new DefaultHandler(); //new PDUHandler();
     pduErrors =  new DefaultHandler(); //new PDUErrorHandler();
     
     
     parser->setContentHandler(pduHandler);
     parser->setErrorHandler(pduErrors);
     
     cout<<"xmlString is "<<xmlString<<endl;
     parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, true);
     try {
          cout<<"Attempting parse..."<<endl;
          parser->parse(*inputSource);
     }
     catch (const XMLException& e)
     {
          string message(XMLString::transcode(e.getMessage()));
          cout<<"XML exception "<<message<<endl;
          return 1;
     }
     catch (const SAXParseException& e)
     {
          string message(XMLString::transcode(e.getMessage()));
          cout<<"SAXParse exception "<<message<<endl;
          return 1;
          
     }
     catch(const exception& e)
     {
          cout <<"Unexpected exception "<<e.what()<<endl;
          return 1;
     }

}
///////////////////////////////////
After the 'parser->parse' call I get this error:

SAXParse exception An exception occurred! Type:UTFDataFormatException,
 Message:invalid byte 1 () of a 1-byte sequence.


Re: UTF error with SAX

Posted by Vitaly Prapirny <ma...@mebius.net>.
Hi,
Coker, Jonathan M wrote:
>      xmlBuffer =  new XMLByte[strlen(xmlString)];
>      memcpy(xmlBuffer,xmlString, strlen(xmlString));
>      
>      MemBufInputSource* inputSource =   new
> MemBufInputSource(xmlBuffer,512,sourceName);  
                               ^^^ strlen(xmlString) instead of 512

Good luck !
	Vitaly