Here is the problem, by default the STL libraries use the operator== method
for any given comparison.

So if you try to use a map like this:

map<xmlChar *, my_object> my_map;

it will compare the pointers themselves, and not the contents of the
pointers.
Its like saying:

xmlChar  *a, *b;
if( a==b ) {
    // do something
}

The previous code is not valid when normally operating on strings.

The workaround is to provide a comparator function to the map object, you
can use the following:

struct xmlCmp {
    bool operator()(const xmlChar* s1, const xmlChar* s2) const {
        return xmlStrcmp(s1, s2) < 0;
    }
};
map<xmlChar *, my_object, xmlCmp> my_better_map;

That should solve your STL problems.

Thanks,

James Wert Jr.
[EMAIL PROTECTED]



|---------+---------------------------->
|         |           Daniel Veillard  |
|         |           <[EMAIL PROTECTED]|
|         |           com>             |
|         |           Sent by:         |
|         |           [EMAIL PROTECTED]|
|         |           .org             |
|         |                            |
|         |                            |
|         |           05/25/2005 03:26 |
|         |           AM               |
|         |           Please respond to|
|         |           veillard         |
|---------+---------------------------->
  
>----------------------------------------------------------------------------------------------------------------------------------|
  |                                                                             
                                                     |
  |       To:       Antti M�kinen <[EMAIL PROTECTED]>                           
                                             |
  |       cc:       [email protected]                                               
                                                     |
  |       Subject:  Re: [xml] Problems with xmlChar* as a key in STL Map        
                                                     |
  
>----------------------------------------------------------------------------------------------------------------------------------|




On Wed, May 25, 2005 at 09:12:08AM +0300, Antti M�kinen wrote:
> If I compare the the acquired xmlChar* with another xmlChar*, which is
> created like...
>
>   xmlChar* test = BAD_CAST "and";
>
> ...with xmlStrcmp, the result is that the strings are the same. This test

> string I can use for searching a certain key in STL Map and it works. Why

> is it, that the xmlChar* acquired with XPath from a node does not work
when
> trying to find a key in STL Map? Does that have something to do with
> encoding or what, or with the method that STL Map uses for comparing
> strings?

  I'm a bit puzzled by your question. I do not know "STL Map", I have no
idea
if this may have an influence. All I can tell is:
   - the xmlChar* string obtained from libxml2 is encoded in UTF-8
     (UTF-8 encoding and ASCII encoding match for characters in the ASCII
range)
   - the string may be deallocated if you free the document or if you
     free the XPath object which was returned, I assume that's the
     problem you're facing.

   otherwise no idea.

Daniel

--
Daniel Veillard      | Red Hat Desktop team http://redhat.com/
[EMAIL PROTECTED]  | libxml GNOME XML XSLT toolkit  http://xmlsoft.org/
http://veillard.com/ | Rpmfind RPM search engine http://rpmfind.net/
_______________________________________________
xml mailing list, project page  http://xmlsoft.org/
[email protected]
http://mail.gnome.org/mailman/listinfo/xml





_______________________________________________
xml mailing list, project page  http://xmlsoft.org/
[email protected]
http://mail.gnome.org/mailman/listinfo/xml

Reply via email to