简体   繁体   中英

LibXml2 with C/C++

This is done using libxml2-2.7.8, I am getting error in it. I have tried with this:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>

int main(int argc, char **argv) {

    char         *docname;
    xmlDocPtr    doc;
    xmlNodePtr   cur;
    xmlChar      *uri;

    if (argc <= 1) {
        printf("Usage: %s docname\n", argv[0]);
        return(0);
    }

    docname = argv[1];

    doc = xmlParseFile(docname);
    cur = xmlDocGetRootElement(doc);

    cur = cur->xmlChildrenNode;
    while (cur != NULL) {
        if ((!xmlStrcmp(cur->name, (const xmlChar *)"reference"))) {
            uri = xmlGetProp(cur, "uri");
            printf("uri: %s\n", uri);
            xmlFree(uri);
        }
        cur = cur->next;
    }
    xmlFreeDoc(doc);
    return (1);
}

and XML file:

<?xml version="1.0" encoding="utf-8"?>
<story>
  <storyinfo>
    <author>John Fleck</author>
    <datewritten>June 2, 2002</datewritten>
    <keyword>تخیلی</keyword>
  </storyinfo>
  <body>
    <headline>This is the headline</headline>
    <para>This is the body text.</para>
  </body>
  <reference uri="www.w3.org/TR/xslt20/"/>
</story>

I am getting error as: error C2664: 'xmlChar *xmlGetProp(xmlNodePtr,const xmlChar *)' : cannot convert argument 2 from 'const char [4]' to 'const xmlChar *'

Please help

您缺少对const xmlChar *的强制转换:

uri = xmlGetProp(cur, (const xmlChar*)"uri");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM