Alberto Massari wrote:
> 3) if you don't option 2, you have to do the conversion at runtime
> using a transcoder, like in
>
>    const XMLCh* gFileToTrap=XMLString::transcode("personal.dtd");
>
> Don't forget to delete the string by using
> XMLString::release(&gFileToTrap); also, you must allocate it after
> XMLPlatformUtils::initialize and deallocate it before
> XMLPlatformUtils::terminate.

Hi Rafael,

I have good experience in using a little wrapper class around XMLCh. I 
called it XML_Char, and I use it like the following examples:

        XML_Char empty;         // empty string
        XML_Char text("hello"); // initialized from const char*
        XML_Char attr = node->getAttribute(XML_Char("id"));
                                // initialized from const XMLCh*
        XML_Char copy(text);    // copy constructor
        empty = text;           // assignment
        text = "go away";       // implicit construction and assignment

Each object of the XML_Char class has a representation in char* and in 
XMLCh*, and the destructor/assignment operator keeps track of freeing 
the memory used. The class has inlined

        operator const XMLCh*() const
        operator const char*() const

referencing operators, so you can use the objects simultaneously as const 
char*- or const XMLCh*-strings. Additionally, I have some convenience 
operators like

        unsigned len() const    // equ. to strlen(.)
        operator bool () const  // test if string is empty
        char operator[](unsigned i) const       // indexing (rarely used)
        friend std::ostream &operator<<(std::ostream &o, const XML_Char &c)
                                // stream output

There are some disadvantages, which must be kept in mind when using 
XML_Char:
        - you can't declare global objects, since XMLPlatformUtils::initialize
          will be called _after_ construction, and the conversion during
          construction will crash the program.
        - there's some overhead, resulting from the dual representation, which
          is not used in every scenario.
        - undefined behaviour when the string contains letters which are not
          representable in the local code page. This can be eliminated by using
          utf-8 as the local encoding.

The reason why I use this class in all my xerces-related projects, are 
the advantages:
        - no memory leaks due to string handling/transcoding
        - automatic transcoding between XMLCh and local code page
        - convenient string handling when dealing with DOM/SAX.
        - the whole class is inlined (header file only)
        - the implementation is optimized for _my_ needs ;)

Rafael, if you are a lazy guy, you may ask me for a copy of the class 
XML_Char - it's open source. But if you are keen, you sit down and write 
your own, it will take you little effort (and you can optimize it for 
_your_ needs).

Cheers,
                        Axel

Reply via email to