libxml2库读取xml文件的部分节点
时间: 2024-02-28 13:41:34 浏览: 60
的示例程序如何实现?
可以参考下面的示例代码:
#include <libxml/parser.h>
#include <libxml/tree.h>
int main() {
xmlDocPtr doc;
xmlNodePtr node;
// load the XML document
doc = xmlReadFile("example.xml", NULL, 0);
if (doc == NULL) {
fprintf(stderr, "Failed to parse XML document\n");
return 1;
}
// find the parent node
node = xmlDocGetRootElement(doc);
if (node == NULL) {
fprintf(stderr, "Empty XML document\n");
xmlFreeDoc(doc);
return 1;
}
// loop through the child nodes
for (node = node->xmlChildrenNode; node != NULL; node = node->next) {
if (xmlStrcmp(node->name, (const xmlChar *)"some_tag") == 0) {
// do something with this node
xmlChar *content = xmlNodeGetContent(node);
printf("Found some_tag with content '%s'\n", content);
xmlFree(content);
}
}
// free the XML document and cleanup
xmlFreeDoc(doc);
xmlCleanupParser();
return 0;
}
希望对你有所帮助。
阅读全文