Like Tree1Likes
  • 1 Post By grumpy

Using libxml2 invalid conversion from const char* to const xmlChar* error

This is a discussion on Using libxml2 invalid conversion from const char* to const xmlChar* error within the C++ Programming forums, part of the General Programming Boards category; Hi I am having this issue and not sure how to resolve. I am having to compile my code using ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    20

    Using libxml2 invalid conversion from const char* to const xmlChar* error

    Hi I am having this issue and not sure how to resolve.

    I am having to compile my code using g++ instead of gcc for a number of reasons, however I am getting:

    main.cpp:129: error: invalid conversion from ‘const char*’ to ‘const xmlChar*’
    main.cpp:129: error: initializing argument 2 of ‘xmlChar* xmlGetProp(xmlNode*, const xmlChar*)’

    Code:
    void getIngredients (xmlDocPtr doc, xmlNodePtr cur) {
    	xmlChar *name;
    	cur = cur->xmlChildrenNode;
    	while (cur != NULL) {
    	    if ((!xmlStrcmp(cur->name, (const xmlChar *)"ingredient"))) {
    		    name = xmlGetProp(cur, "name");
    		    printf("name: %s\n", name);
    		    xmlFree(name);
    	    }
    Has anyone got any ideas??

  2. #2
    Registered User
    Join Date
    Oct 2007
    Posts
    20
    Not to worry - sorted it....
    xmlGetProp(cur, "name");
    "name" needed to be casted so that it said xmlGetProp(cur, (const xmlChar *)"name");

    Thanks

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,169
    Change the offending line to
    Code:
         name = xmlGetProp(cur, (const xmlChar *)"name");
    The preceding line does exactly the same type of conversion, in a similar context, so I assume it is valid. However, you will need to confirm that (eg through appropriate testing). Shutting the compiler up does not always correspond to doing what is intended.
    pc_doctor likes this.
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    20
    Grumpy - thanks for that.

    Many thanks.
    Last edited by pc_doctor; 04-20-2011 at 09:27 AM.

  5. #5
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    xmlChar is typedef'd as an unsigned char, so it should not be a problem casting between xmlChar * and char *.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "invalid conversion from 'char' to 'const char' "
    By gurharman in forum C++ Programming
    Replies: 8
    Last Post: 11-24-2010, 09:43 AM
  2. invalid conversion from 'const char*' to 'char'
    By howzer in forum C++ Programming
    Replies: 6
    Last Post: 03-15-2006, 11:58 AM
  3. invalid conversion from `const char*' to `char'
    By GameGenie in forum C++ Programming
    Replies: 6
    Last Post: 08-01-2005, 05:30 AM
  4. Invalid conversion from 'const void*' to 'void*' error
    By prawntoast in forum C Programming
    Replies: 3
    Last Post: 05-01-2005, 10:30 AM
  5. invalid conversion from 'char' to 'const char*'
    By LiKWiD in forum C++ Programming
    Replies: 10
    Last Post: 03-20-2005, 02:50 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21