我只是想读取之前写出文件的 XML 节点内容中的字符串。这是代码:
int main() {
xmlNodePtr n, n2, n3;
xmlDocPtr doc;
xmlChar *xmlbuff;
int buffersize;
xmlChar* key;
doc = xmlNewDoc(BAD_CAST "1.0");
n = xmlNewNode(NULL, BAD_CAST "root");
xmlNodeSetContent(n, BAD_CAST "test1");
n2 = xmlNewNode(NULL, BAD_CAST "devices");
xmlNodeSetContent(n2, BAD_CAST "test2");
n3 = xmlNewNode(NULL, BAD_CAST "device");
xmlNodeSetContent(n3, BAD_CAST "test3");
//n2 = xmlDocCopyNode(n2, doc, 1);
xmlAddChild(n2,n3);
xmlAddChild(n,n2);
xmlDocSetRootElement(doc, n);
xmlSaveFormatFileEnc( FILENAME, doc, "utf-8", 1 );
doc = xmlParseFile(FILENAME);
n = xmlDocGetRootElement(doc);
key = xmlNodeListGetString(doc, n, 1);
printf("keyword: %s\n", key);
xmlFree(key);
n = n->children;
key = xmlNodeListGetString(doc, n, 1);
printf("keyword: %s\n", key);
xmlFree(key);
n = n->children;
key = xmlNodeListGetString(doc, n, 1);
printf("keyword: %s\n", key);
xmlFree(key);
n2 = xmlNewNode(NULL, BAD_CAST "address");
xmlAddChild(n,n2);
xmlDocSetRootElement(doc, n);
xmlSaveFormatFileEnc( FILENAME, doc, "utf-8", 1 );
return 0;
}
此代码的输出是 -> 关键字:(null) 关键字:test1 关键字:(null)
为什么我看不到 test2 和 test3?
提前致谢。