【发布时间】:2015-02-25 10:46:42
【问题描述】:
我用C++写了一个程序来演示libxml2的使用,代码如下
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
void parseStory (xmlDocPtr doc, xmlNodePtr cur) {
xmlChar *key;
cur = cur->xmlChildrenNode;
while (cur != NULL) {
if ((!xmlStrcmp(cur->name, (const xmlChar *)"keyword"))) {
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
printf("keyword: %s\n", key);
xmlFree(key);
}
cur = cur->next;
}
return;
}
static void parseDoc(char *docname) {
xmlDocPtr doc;
xmlNodePtr cur;
doc = xmlParseFile(docname);
if (doc == NULL ) {
fprintf(stderr,"Document not parsed successfully. \n");
return;
}
cur = xmlDocGetRootElement(doc);
if (cur == NULL) {
fprintf(stderr,"empty document\n");
xmlFreeDoc(doc);
return;
}
if (xmlStrcmp(cur->name, (const xmlChar *) "story")) {
fprintf(stderr,"document of the wrong type, root node != story");
xmlFreeDoc(doc);
return;
}
cur = cur->xmlChildrenNode;
while (cur != NULL) {
if ((!xmlStrcmp(cur->name, (const xmlChar *)"storyinfo"))){
parseStory (doc, cur);
}
cur = cur->next;
}
xmlFreeDoc(doc);
return;
}
int main(int argc, char **argv) {
char *docname;
if (argc <= 1) {
printf("Usage: %s docname\n", argv[0]);
return(0);
}
docname = argv[1];
parseDoc (docname);
return (1);
}
我使用的是 Ubuntu,我已经安装了 libxml2-dev 包并使用
编译g++ Libxml2Example.cpp -I/usr/include/libxml2/libxml -lxml2 -o output
但我收到以下错误
Build of configuration Debug for project Libxml2Example ****
make all
Building file: ../src/Libxml2Example.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/Libxml2Example.d" - MT"src/Libxml2Example.d" -o"src/Libxml2Example.o" "../src/Libxml2Example.cpp"
../src/Libxml2Example.cpp:12:30: fatal error: libxml/xmlmemory.h: No such file or directory
compilation terminated.
make: *** [src/Libxml2Example.o] Error 1
如何解决这个错误?请帮助我。
【问题讨论】:
-
您使用哪种操作系统?您需要在编译中添加 -I。
-
我在 ubuntu 中运行这个
-
请原谅我,但在
c++中使用#include <stdio.h>不是非常规的吗?恕我直言,您应该考虑使用#include <cstdio>。