用libxml2解析XML文件的最快方法
Fastest way to parse a XML file with libxml2?
嗨,有没有" 更快"用libxml2解析XML文件的方法?现在,我按照C 代码这样做:
void parse_element_names(xmlNode * a_node, int *calls)
{
xmlNode *cur_node = NULL;
for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
(*calls)++;
if(xmlStrEqual(xmlCharStrdup("to"),cur_node->name)){
//printf("node type: <%d>, name <%s>, content: <%s> n", cur_node->children->type, cur_node->children->name, cur_node->children->content);
//do something with the content
parse_element_names(cur_node->children->children,calls);
}
else if(xmlStrEqual(xmlCharStrdup("from"),cur_node->name)) {
//printf("node type: <%d>, name <%s>, content: <%s> n", cur_node->children->type, cur_node->children->name, cur_node->children->content);
//do something with the content
parse_element_names(cur_node->children->children,calls);
}
else if(xmlStrEqual(xmlCharStrdup("note"),cur_node->name)) {
//printf("node type: <%d>, name <%s>, content: <%s> n", cur_node->children->type, cur_node->children->name, cur_node->children->content);
//do something with the content
parse_element_names(cur_node->children->children,calls);
}
.
.
.
//about 100 more node names comming
else{
parse_element_names(cur_node->children,calls);
}
}
}
int main(int argc, char **argv)
{
xmlDoc *doc = NULL;
xmlNode *root_element = NULL;
if (argc != 2)
return(1);
/*parse the file and get the DOM */
doc = xmlReadFile(argv[1], NULL, XML_PARSE_NOBLANKS);
if (doc == NULL) {
printf("error: could not parse file %sn", argv[1]);
}
int calls = 0;
/*Get the root element node */
root_element = xmlDocGetRootElement(doc);
parse_element_names(root_element,&calls);
/*free the document */
xmlFreeDoc(doc);
xmlCleanupParser();
return 0;
}
真的是最快的方法吗?还是有更好/更快的解决方案,您可以建议我?
谢谢
xmlReadFile
等。基于Libxml2的SAX Parser接口(实际上是SAX2接口),因此,如果您不需要生成的xmlDoc
。
如果您必须区分示例中的许多不同元素名称,则最快的方法通常是为每种类型的节点创建单独的功能,并使用哈希表来查找这些函数。
相关文章:
- 如何使用curlpp通过POST方法上传文件和json数据
- 一种在C++中读取TXT配置文件的简单方法
- 在 cpp 文件中隐藏采用模板参数引用的方法
- 在C++中包含原型文件的正确方法是什么?
- 通过比较C++中的行在 txt 文件中搜索的最简单方法是什么?
- 不带预处理器的调用方法/文件的文件名/行号
- 引用文件的适当方法是什么?
- 在文件中写入而不是在 c++ 中使用 "<<" 的替代方法?
- 查找定义我的 C/C++ 函数/宏的文件比'grep'更简单的方法
- 读取大文件(>2GB)(文本文件包含以太网数据)并通过不同参数随机访问数据的最佳方法是什么?
- 在C++中创建文件夹选取器对话框的最简单方法是什么?
- C++:std::ofstream 方法 open() 在第二次迭代时擦除打开的 ifstream 文件
- 如何使用 C/C++ 和 system() 系统调用以外的其他方法在 Linux 中获取文件功能?
- 创建进行生产构建并创建调试信息的C++生成文件的最佳方法?
- 是否有正确的方法对生成文件中的对象文件使用模板命令?(C++)
- 将位字符串转储到二进制文件的最佳方法是什么
- 在目录中查找所有.cpp.h文件(include,src等)的传统方法
- 通过boost asio iostream下载大文件的最快方法是什么?
- LNK2019 在文件中调用静态成员的方法时出错.cpp
- 在 *.cpp 文件中实现的 c++ 函数/方法永远不会内联扩展吗?