forwarded mail:


i am trying to write the xml document in to a file in UTF-8 encoding here 
below 
is the code i wrote for it, i do not understand whats wrong but its not 
functioning as i expected, the file format after domwriter puts the output 
is 
still ANSI, please let me know whats wrong

//// -------xe_domparser_demo.cpp
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/dom/DOM.hpp>
#include <xercesc\util\XMLUniDefs.hpp>
#include <xercesc/framework/LocalFileFormatTarget.hpp>

// my error handler
#include <XP/XPErrorHandler.h>

#include <iostream.h>
#include <assert.h>
XERCES_CPP_NAMESPACE_USE

int main (int argc, char* argv[]) 
{
        int result = 0;
        if (argc < 2)
        {
                cout << "Usage " << argv[0] << " <XML File> " << endl;
                return 0;
        }

        char* xmlFile = argv[1];

        try 
        {
                XMLPlatformUtils::Initialize();
        }
        catch (const XMLException& toCatch) 
        {
                char* message = 
XMLString::transcode(toCatch.getMessage());
                cout << "Error during initialization! " << endl
                     << message << endl;
                XMLString::release(&message);
                return 1;
        }

        static const XMLCh gLS[] = { chLatin_L, chLatin_S, chNull }; 
        DOMImplementationLS *domImplimentation = 
DOMImplementationRegistry::getDOMImplementation(gLS); 
        DOMBuilder* domBuilder = domImplimentation->createDOMBuilder
(DOMImplementationLS::MODE_SYNCHRONOUS, NULL); 
        
        if ( domBuilder == NULL)
        {
                cout << "Error creating DOMBuilder, can't continue...!\n";
                return -1;
        }
        
        XPErrorHandler* domErrorHandler = new XPErrorHandler 
("xp_error.log"); // my error handler
        domBuilder->setErrorHandler  (domErrorHandler);
        domErrorHandler->resetErrors();

        //domBuilder->resetDocumentPool ();

        domBuilder->setFeature(XMLUni::fgDOMNamespaces, true);
        domBuilder->setFeature(XMLUni::fgXercesSchema, true);
        domBuilder->setFeature(XMLUni::fgXercesSchemaFullChecking, true);
        domBuilder->setFeature(XMLUni::fgDOMValidateIfSchema, true);
        domBuilder->setFeature(XMLUni::fgDOMDatatypeNormalization, true);

        DOMDocument* domDocument = NULL;
        try 
        {               
                domDocument = domBuilder->parseURI(xmlFile);
                
                if (domErrorHandler->GetErrorCount() > 0)
                {
                        domErrorHandler->LogToFile ("Parsing compleeted 
with " 
+ MakeString(domErrorHandler->GetErrorCount()) + " number of errors");          
                        cout << "Parsed the file: " << xmlFile << " with " 
<< 
domErrorHandler->GetErrorCount() << " number of errors \n";
                        cout << (domErrorHandler->GetErrors ()).data () << 
endl;
                }
                else 
                {
                        domErrorHandler->LogToFile ("Parsing compleeted 
successfully without any error");
                        cout << "Parsed the file: " << xmlFile << " 
without any 
parse erros \n";
                }
        }
        catch (SAXException  &saxException)
        {
                //Any SAX exception, possibly wrapping another exception.  
                char* message = 
XMLString::transcode(saxException.getMessage 
());
                cout << "SAX Exception in parsing : " << message << endl;
                XMLString::release(&message);
                return -1;
        }
        catch (XMLException  &xmlException)
        {
                //An exception from the parser or client handler code.  
                char* message = 
XMLString::transcode(xmlException.getMessage 
());
                cout << "XML Exception in parsing : " << message << endl;
                XMLString::release(&message);
                return -1;
        }
        catch (DOMException  &domException)
        {
                //A DOM exception  
                char* message = XMLString::transcode(domException.msg);
                cout << "DOM Exception in parsing : " << message << endl;
                XMLString::release(&message);
                return -1;
        }

        char* specifiedEncoding = XMLString::transcode (domDocument-
>getEncoding ());
        char* actualEncoding = XMLString::transcode (domDocument-
>getActualEncoding ());
        
        if (specifiedEncoding == NULL)
                cout << "No encoding specified " << endl;
        else
                cout << "Specified encoding: " << specifiedEncoding << 
endl;
        
        if (actualEncoding == NULL)
                cout << "Unable to find actual encoding " << endl;
        else
                cout << "The actual encoding: " << actualEncoding << endl;
        
        // Get the root element of the document
        DOMElement* element = domDocument->getDocumentElement();
        
        cout << "Root element name: " << XMLString :: transcode (element-
>getTagName ()) << endl;
        
        /*if (domErrorHandler->GetErrorCount() == 0)
        {
                
                DOMNodeList* nodeList = domDocument->getChildNodes ();
                assert(nodeList);
                
                //cout << "Length: " << nodeList->getLength () << endl;
                
                for (int i =0; i < nodeList->getLength (); i++)
                {
                        DOMNode* aNode = nodeList->item (i);
                        DisplayDOM (aNode);
                }
        }*/

        DOMWriter* domWriter = domImplimentation->createDOMWriter ();
        assert(domWriter);

        XPErrorHandler* domWriterErrorHandler = new XPErrorHandler 
("xp_writer_error.log");
        domWriterErrorHandler->resetErrors();

        domWriter->setErrorHandler  (domWriterErrorHandler);

        unsigned short* encoding = XMLString::transcode("UTF-16");
        domWriter->setEncoding (encoding); 

        char* outPutFile = "MY_XE_XML_FILE.XML";
        LocalFileFormatTarget localFileFormatTarget(outPutFile);

        domWriter->writeNode(&localFileFormatTarget, *domDocument);
        
        domWriter->release ();
        domBuilder->release (); // call this before platform 
initializations 
are terminated  
        XMLPlatformUtils::Terminate(); // This should be called in the end 

        return 0;
}

void DisplayDOM (DOMNode* node)
{
        short nodeType = node->getNodeType ();

        switch (nodeType)
        {
        case DOMNode::ELEMENT_NODE:
                {
                        DOMElement* element = (DOMElement*)(node);
                        assert(element);
                        cout << "<" <<  XMLString::transcode (element-
>getTagName ()); 
                        
                        DOMNamedNodeMap* attributeMap = 
element->getAttributes 
();
                        assert(attributeMap);

                        for (int i = 0; i < attributeMap->getLength (); 
i++)
                        {
                                DOMAttr* attribute =  
(DOMAttr*)(attributeMap-
>item (i));
                                assert (attribute);

                                cout << " " << XMLString::transcode 
(attribute-
>getName())  
                                         << "=\"" << XMLString::transcode 
(attribute->getValue ()) << "\"";
                        }
                        
                        cout << ">";

                        DOMNodeList* nodeList = element->getChildNodes ();
                        assert(nodeList);
                        
                        int childNodesLength = nodeList->getLength ();
                        for (i =0; i < childNodesLength; i++)
                        {
                                DOMNode* aNode = nodeList->item (i);
                                DisplayDOM (aNode);
                        }
                        
                        cout << "</" <<  XMLString::transcode (element-
>getTagName ()) << ">";

                        element = 0;
                        break;
                }
        case DOMNode::ATTRIBUTE_NODE:
                {
                        DOMAttr* attr = (DOMAttr*)(node);
                        cout << "Attribute name: " << XMLString::transcode 
(attr->getName()) << endl
                                 << "Attribute value: " << 
XMLString::transcode 
(attr->getValue()) << endl;
                        break;
                }
        case DOMNode::TEXT_NODE:
                {
                        DOMText* textNode = (DOMText*)(node);
                        try
                        {
                                char* textValue = 
XMLString::transcode(textNode-
>getTextContent ()); // now y the hell is it aborting here ????
                                if (textValue != NULL)
                                        cout << textValue;
                        }
                        catch (DOMException& domException)
                        {
                                //A DOM exception  
                                char* message = XMLString::transcode
(domException.msg);
                                cout << "DOM Exception in parsing : " << 
message << endl;
                                XMLString::release(&message);
                        }
                        break;
                }
        case DOMNode::CDATA_SECTION_NODE:
                {
                        DOMCDATASection* domCdataSection = 
(DOMCDATASection*)
(node);
                        cout << "CDATA Text section's whole text: " << 
XMLString::transcode(domCdataSection->getWholeText ()) << endl;
                        break;
                }
        case DOMNode::ENTITY_REFERENCE_NODE:
                break;
        case DOMNode::ENTITY_NODE:
                {
                        DOMEntity* entityNode = (DOMEntity*)(node);
                        cout << "Public Id: " << XMLString::transcode 
(entityNode->getPublicId ()) << endl
                                 << "System Id: " << XMLString::transcode 
(entityNode->getSystemId ()) << endl;
                        break;
                }
        case DOMNode::PROCESSING_INSTRUCTION_NODE:
                break;
        case DOMNode::COMMENT_NODE:
                break;
        case DOMNode::DOCUMENT_NODE:
                break;
        case DOMNode::DOCUMENT_TYPE_NODE:
                break;
        case DOMNode::DOCUMENT_FRAGMENT_NODE:
                break;
        case DOMNode::NOTATION_NODE:
                break;
        default:
                break;
        }
}
// end



-- 
Gareth Reakes, Head of Product Development  +44-1865-203192
DecisionSoft Limited                        http://www.decisionsoft.com
XML Development and Services




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to