Be careful, you are leaking the transcoder 't' and the array 'sizes'.
Also, you could store the transcoder in a global variable to avoid allocating/deallocating for each operation.

Alberto

At 23.09 16/07/2007 +0200, Kuisathaverat wrote:
thanks i make a function that transcode from ISO8859-1

and replace X("") for it , i send the code to future
people that have the same problem

#include <stdio.h>
#include <stdlib.h>
#include  <string.h>
#include <ctype.h>
#include <sstream>
#include <iostream>
#include "xercesc/util/PlatformUtils.hpp"
#include "xercesc/util/TransService.hpp"
#include <xercesc/util/XMLString.hpp>
#include <xalanc/Include/PlatformDefinitions.hpp>

#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/framework/LocalFileInputSource.hpp>
#include <xercesc/framework/LocalFileFormatTarget.hpp>
#include <xercesc/framework/MemBufInputSource.hpp>
#include
<xalanc/XercesParserLiaison/XercesDocumentWrapper.hpp>
#include
<xalanc/XercesParserLiaison/XercesWrapperNavigator.hpp>
#include <xercesc/dom/DOM.hpp>
#include <xercesc/dom/DOMDocument.hpp>
#include <xercesc/sax/SAXParseException.hpp>
#include <xercesc/util/XMLUniDefs.hpp>
#include <xercesc/util/XMLUni.hpp>
#include <xercesc/framework/StdInInputSource.hpp>
#include <xercesc/framework/StdOutFormatTarget.hpp>
#include <xercesc/framework/XMLBuffer.hpp>
#include <xercesc/dom/DOMElement.hpp>
#include <xercesc/dom/DOMWriter.hpp>
#include <xercesc/dom/DOMNodeList.hpp>
#include <xercesc/dom/DOMNode.hpp>
#include <xercesc/parsers/XercesDOMParser.hpp>
#include <xercesc/parsers/SAXParser.hpp>
#include <xercesc/util/OutOfMemoryException.hpp>
#include <xercesc/sax/InputSource.hpp>
#include <locale.h>

using namespace std;
using namespace XERCES_CPP_NAMESPACE;


#define X(p) xercesc::XMLString::transcode(p)

XMLCh *transcodeFromISO88591(string sCadena){
        XMLTransService::Codes failReason;
        XMLTranscoder* t =

XMLPlatformUtils::fgTransService->makeNewTranscoderFor("ISO8859-1",
failReason,
                        16*1024,
xercesc::XMLPlatformUtils::fgMemoryManager);
        unsigned int srcLen=sCadena.length();
        int destLen=srcLen+10;
        unsigned char* sizes = new unsigned char[destLen+1];
        XMLCh *dest=0;
        dest=(XMLCh*)calloc(destLen+1,sizeof(XMLCh));
        unsigned int eaten=0;
        unsigned int endDest=0;
        endDest=t->transcodeFrom((const
XMLByte*)sCadena.c_str(),srcLen,(XMLCh
*)dest,destLen,eaten,sizes);
        return dest;
}

 void initXerces(){
        // Initialize the XML4C2 system
        try
        {
                xercesc::XMLPlatformUtils::Initialize();
        }
        catch(const XMLException &toCatch)
        {
                XERCES_STD_QUALIFIER cerr << "Error during Xerces-c
Initialization.\n"
                                << "  Exception message:"
                        << X(toCatch.getMessage()) << XERCES_STD_QUALIFIER
endl;
                throw std::runtime_error("Can No ini Xerces");
        }

 }

void endXerces(){
        // Initialize the XML4C2 system
        try
        {
                xercesc::XMLPlatformUtils::Terminate();
        }

        catch(const XMLException &toCatch)
        {
                XERCES_STD_QUALIFIER cerr << "Error during Xerces-c
Initialization.\n"
                                << "  Exception message:"
                        << X(toCatch.getMessage()) << XERCES_STD_QUALIFIER
endl;
                throw std::runtime_error("Can No ini Xerces");
        }

 }
DOMDocument *createDocument(string sRootElem, string
sEncoding){
        DOMImplementation* impl =
DOMImplementationRegistry::getDOMImplementation(transcodeFromISO88591("Core"));
DOMDocument* pXMLDoc = NULL;
        if (impl != NULL)
        {
                try
                {
                        pXMLDoc = impl->createDocument(
0, // root element namespace
URI.

transcodeFromISO88591(sRootElem),         // root
element name
                        0);                   // document type object
(DTD).

pXMLDoc->setEncoding(transcodeFromISO88591(sEncoding));
//
pXMLDoc->setActualEncoding(X(sEncoding.c_str()));
//                      pXMLDoc->setVersion(X("1.1"));
                        DOMText*    pText =
pXMLDoc->createTextNode(transcodeFromISO88591("níñó"));
                        pXMLDoc->getDocumentElement()->appendChild(pText);
                }
                catch (...)
                {
                        throw std::runtime_error("Error: creating
document");
                }
        }  // (inpl != NULL)
        else
        {
                throw std::runtime_error("Error: no
implementation");
        }
        return pXMLDoc;
 }

DOMElement *createNode(DOMDocument *pXMLDoc,string
sName){
DOMElement*  pNode = NULL;
 try{
        pNode =
pXMLDoc->createElement(transcodeFromISO88591(sName));
        pXMLDoc->getDocumentElement()->appendChild(pNode);
 }catch(...){}
return pNode;
}

void setAttr(DOMElement *pNodeElem,string sName,string
sValue){

pNodeElem->setAttribute(transcodeFromISO88591(sName),transcodeFromISO88591(sValue));
}

bool saveFile(DOMDocument *pXMLDoc,string
sFileName,string sEncoding){
        bool bRet = true;
        try
        {
                // get a serializer, an instance of DOMWriter

                DOMImplementation *impl          =
pXMLDoc->getImplementation();
                DOMWriter         *theSerializer =
impl->createDOMWriter();

            // set user specified output encoding

theSerializer->setEncoding(transcodeFromISO88591(sEncoding));

            // set feature if the serializer supports
the feature/mode

//              if
(theSerializer->canSetFeature(XMLUni::fgDOMWRTEntities,
true))
//
theSerializer->setFeature(XMLUni::fgDOMWRTEntities,
true);
//              if
(theSerializer->canSetFeature(XMLUni::fgDOMWRTNormalizeCharacters,
true))
//
theSerializer->setFeature(XMLUni::fgDOMWRTNormalizeCharacters,
true);

//              if
(theSerializer->canSetFeature(XMLUni::fgDOMWRTSplitCdataSections,
true))
//
theSerializer->setFeature(XMLUni::fgDOMWRTSplitCdataSections,
true);

//              if
(theSerializer->canSetFeature(XMLUni::fgDOMWRTDiscardDefaultContent,
true))
//
theSerializer->setFeature(XMLUni::fgDOMWRTDiscardDefaultContent,
true);

                if
(theSerializer->canSetFeature(XMLUni::fgDOMWRTFormatPrettyPrint,
true))

theSerializer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint,
true);

//              if
(theSerializer->canSetFeature(XMLUni::fgDOMWRTBOM,
true))
//                      theSerializer->setFeature(XMLUni::fgDOMWRTBOM,
true);
                //
            // Plug in a format target to receive the
resultant
            // XML stream from the serializer.
                //
            // StdOutFormatTarget prints the resultant
XML stream
            // to stdout once it receives any thing
from the serializer.
                //
                XMLFormatTarget *myFormTarget;
//              myFormTarget = new
LocalFileFormatTarget(sFileName.c_str());
                myFormTarget = new StdOutFormatTarget();

            // get the DOM representation
                pXMLDoc->normalizeDocument();
                DOMNode                     *doc = (DOMNode
*)pXMLDoc;
                //
            // do the serialization through
DOMWriter::writeNode();
                //
                theSerializer->writeNode(myFormTarget, *doc);


                delete theSerializer;

                //
            // Filter, formatTarget and error handler
            // are NOT owned by the serializer.
                //
                delete myFormTarget;
        }
        catch (const OutOfMemoryException&)
        {
                XERCES_STD_QUALIFIER cerr << "OutOfMemoryException"
<< XERCES_STD_QUALIFIER endl;
                bRet=false;
        }
        catch (XMLException& e)
        {
                XERCES_STD_QUALIFIER cerr << "An error occurred
during creation of output transcoder. Msg is:"
                                << XERCES_STD_QUALIFIER endl
                        << X(e.getMessage()) << XERCES_STD_QUALIFIER endl;
                bRet=false;
        }

        return bRet;
}

int main(int argc, char *argv[])
{
initXerces();
DOMDocument *pXMLDoc =
createDocument("DocRoot","UTF-8");
DOMElement *pNodeElem=0;
pNodeElem = createNode(pXMLDoc,"Node");
setAttr(pNodeElem,"id","1");
setAttr(pNodeElem,"desc","níñó");
saveFile(pXMLDoc,"/tmp/sample.xml","UTF-8");
saveFile(pXMLDoc,"/tmp/sample.xml","ISO-8859-1");
saveFile(pXMLDoc,"/tmp/sample.xml","ISO-8859-15");
endXerces();
return EXIT_SUCCESS;
}

--- Alberto Massari <[EMAIL PROTECTED]>
escribió:

> You should not use X("ñíñó"), as
> XMLString::transcode assumes that the given
> string is written in the current locale; if you
> are not using a Spanish machine you will get
> garbage Unicode characters (probably, '?') that
> each output encoding will render with a character
> entity. You should use the appropriate transcoder
> to convert your strings into Unicode.
>
> Alberto
>
> At 13.26 14/07/2007 +0200, Kuisathaverat wrote:
> >I have used DOMWriter->WriteNode() to write
> documens
> >that previusly i parse and modify , with this
> >documents i no have problems with the output
> >characters in iso8859-1 or iso8859-15 or utf-8
> >encoding but when i make the document  with
> >DOMImplementation the especial characteras as á or
> ñ
> >will lost in ther write to disk.
> >I make a short example to ilustrate it. The correct
> >output was :
> ><DocRoot>níñó
> ><Node id="1" desc="níñó" />
> ></DocRoot>
> >
> >but i obtain :
> >
> >in UTF-8
> >
> ><?xml version="1.0" encoding="UTF-8"
> standalone="no"
> >?>
> ><DocRoot xmlns="">&#65533;&#65533;&#65533;&#65533;
> >
> >   <Node desc="&#65533;&#65533;&#65533;&#65533;"
> >id="1"/>
> >
> ></DocRoot>
> >
> >in iso8859-1
> >
> ><?xml version="1.0" encoding="ISO-8859-1"
> >standalone="no" ?>
> ><DocRoot xmlns="">&#65533;&#65533;&#65533;&#65533;
> >
> >   <Node desc="&#65533;&#65533;&#65533;&#65533;"
> >id="1"/>
> >
> ></DocRoot>
> >
> >in iso8859-15
> >
> ><?xml version="1.0" encoding="ISO-8859-15"
> >standalone="no" ?>
> ><DocRoot xmlns="">&#65533;&#65533;&#65533;&#65533;
> >
> >   <Node desc="&#65533;&#65533;&#65533;&#65533;"
> >id="1"/>
> >
> ></DocRoot>
> >
> >I know that i do some thin wrong but i do no know ,
> i
> >search in mailllist but i no found solution or
> similar
> >problem.
> >
> >it is the code
> >
> >#include <stdio.h>
> >#include <stdlib.h>
> >#include  <string.h>
> >#include <ctype.h>
> >#include <sstream>
> >#include <iostream>
> >#include "xercesc/util/PlatformUtils.hpp"
> >#include "xercesc/util/TransService.hpp"
> >#include <xercesc/util/XMLString.hpp>
> >#include <xalanc/Include/PlatformDefinitions.hpp>
> >
> >#include <xercesc/util/PlatformUtils.hpp>
> >#include
> <xercesc/framework/LocalFileInputSource.hpp>
> >#include
> <xercesc/framework/LocalFileFormatTarget.hpp>
> >#include <xercesc/framework/MemBufInputSource.hpp>
> >#include
>
><xalanc/XercesParserLiaison/XercesDocumentWrapper.hpp>
> >#include
>
><xalanc/XercesParserLiaison/XercesWrapperNavigator.hpp>
> >#include <xercesc/dom/DOM.hpp>
> >#include <xercesc/dom/DOMDocument.hpp>
> >#include <xercesc/sax/SAXParseException.hpp>
> >#include <xercesc/util/XMLUniDefs.hpp>
> >#include <xercesc/util/XMLUni.hpp>
> >#include <xercesc/framework/StdInInputSource.hpp>
> >#include <xercesc/framework/StdOutFormatTarget.hpp>
> >#include <xercesc/framework/XMLBuffer.hpp>
> >#include <xercesc/dom/DOMElement.hpp>
> >#include <xercesc/dom/DOMWriter.hpp>
> >#include <xercesc/dom/DOMNodeList.hpp>
> >#include <xercesc/dom/DOMNode.hpp>
> >#include <xercesc/parsers/XercesDOMParser.hpp>
> >#include <xercesc/parsers/SAXParser.hpp>
> >#include <xercesc/util/OutOfMemoryException.hpp>
> >#include <xercesc/sax/InputSource.hpp>
> >
> >using namespace std;
> >using namespace XERCES_CPP_NAMESPACE;
> >
> >
> >#define X(p) xercesc::XMLString::transcode(p)
> >
> >  void initXerces(){
> >         // Initialize the XML4C2 system
> >         try
> >         {
> >
> xercesc::XMLPlatformUtils::Initialize();
> >         }
> >
> >         catch(const XMLException &toCatch)
> >         {
> >                 XERCES_STD_QUALIFIER cerr <<
> "Error during Xerces-c
> >Initialization.\n"
> >                                 << "  Exception
> message:"
> >                                 <<
> X(toCatch.getMessage()) <<
> >XERCES_STD_QUALIFIER endl;
> >                 throw std::runtime_error("Can No
> ini Xerces");
> >         }
> >
> >  }
> >
> >void endXerces(){
> >         // Initialize the XML4C2 system
> >         try
> >         {
> >
> xercesc::XMLPlatformUtils::Terminate();
> >         }
> >
> >         catch(const XMLException &toCatch)
> >         {
> >                 XERCES_STD_QUALIFIER cerr <<
> "Error during Xerces-c
> >Initialization.\n"
> >                                 << "  Exception
> message:"
> >                                 <<
> X(toCatch.getMessage()) <<
> >XERCES_STD_QUALIFIER endl;
> >                 throw std::runtime_error("Can No
> ini Xerces");
> >         }
> >
> >  }
> >DOMDocument *createDocument(string sRootElem,
> string
> >sEncoding){
> >DOMImplementation* impl =
>
>DOMImplementationRegistry::getDOMImplementation(X("Core"));
> >DOMDocument* pXMLDoc = NULL;
> >         if (impl != NULL)
> >         {
> >                 try
> >                 {
> >                         pXMLDoc =
> impl->createDocument(
> >                                         0,
> >                // root element namespace
> >URI.
> >                         X(sRootElem.c_str()),
>    // root element name
> >                         0);                   //
> document type object
> >(DTD).
> >
> pXMLDoc->setEncoding(X(sEncoding.c_str()));
> >//
> >pXMLDoc->setActualEncoding(X(sEncoding.c_str()));
> >//
> pXMLDoc->setVersion(X("1.1"));
> >                         DOMText*    pText =
> >pXMLDoc->createTextNode(X("ñíñó"));
> >
> pXMLDoc->getDocumentElement()->appendChild(pText);
> >                 }
> >                 catch (...)
> >                 {
> >                         throw
> std::runtime_error("Error: creating
> >document");
> >                 }
> >         }  // (inpl != NULL)
> >         else
>
=== message truncated ===


Un Saludo
kuisathaverat

Solo hay 10 tipos de personas, las que saben binario y las que no

http://kuisat.no-ip.org
http://www.fortunecity.com/campus/physics/114
http://kuisat.infierno.org
ICQ #9368869
Linux User #107185





______________________________________________
LLama Gratis a cualquier PC del Mundo.
Llamadas a fijos y móviles desde 1 céntimo por minuto.
http://es.voice.yahoo.com

Reply via email to