这是使用 libxml2-2.7.8 完成的,我在其中遇到错误。 我试过这个:
#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);
}
和 XML 文件:
<?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>
我得到的错误是: 错误 C2664:“xmlChar *xmlGetProp(xmlNodePtr,const xmlChar *)”:无法将参数 2 从“const char [4]”转换为“const xmlChar *”
请帮忙
最佳答案
您缺少对 const xmlChar* 的转换:
uri = xmlGetProp(cur, (const xmlChar*)"uri");
关于c - 使用 C/C++ 的 LibXml2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36301126/