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

  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,815
    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.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  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
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    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, 10:43 AM
  2. invalid conversion from 'const char*' to 'char'
    By howzer in forum C++ Programming
    Replies: 6
    Last Post: 03-15-2006, 12:58 PM
  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, 03:50 AM