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="">&#xFFFD;&#xFFFD;&#xFFFD;&#xFFFD;

  <Node desc="&#xFFFD;&#xFFFD;&#xFFFD;&#xFFFD;"
id="1"/>

</DocRoot>

in iso8859-15

<?xml version="1.0" encoding="ISO-8859-15"
standalone="no" ?>
<DocRoot xmlns="">&#xFFFD;&#xFFFD;&#xFFFD;&#xFFFD;

  <Node desc="&#xFFFD;&#xFFFD;&#xFFFD;&#xFFFD;"
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
        {
                throw std::runtime_error("Error: no
implementation");
        }
        return pXMLDoc;
 }

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

void setAttr(DOMElement *pNodeElem,string sName,string
sValue){
pNodeElem->setAttribute(X(sName.c_str()),X(sValue.c_str()));
}

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(X(sEncoding.c_str()));

            // 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","ñíñó");
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;
}


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



____________________________________________________________________________________
Sé un Mejor Amante del Cine
¿Quieres saber cómo? ¡Deja que otras personas te ayuden!
http://advision.webevents.yahoo.com/reto/entretenimiento.html

Reply via email to