使用pugixml开源库接口并扩展,解析Word 文字和table 上下行位置关系
1.遍历指定节点下所有节点,包括当前节点下各个层的节点,判断某个节点的存在否
2. find_node()函数使用,predicate 参数构造传入方法
3. 获取当前word文档的格式,并按照原文将文字段落和表格的上下行位置关系还原所以需要确认纯文字段落的位置和table的位置
std::vector<std::string> duckx::Document::get_elements() const
{std::vector<std::string> allElements;pugi::xml_object_range<pugi::xml_node_iterator> childs = document.child("w:document").child("w:body").children();struct allow_remote_predicate{bool operator()(pugi::xml_node node) const{return strcmp(node.name(), "w:t") == 0;}};for (auto& it_child : childs) {if ( strcmp(it_child.name() , "w:p") == 0 ){ // w:body/ w:p / w:r/ w:t="ass"if(it_child.find_node(allow_remote_predicate()) != nullptr ) {printf("find a *** %s \n",it_child.find_node(allow_remote_predicate()).name());allElements.push_back("w:p");continue;}}else if (strcmp(it_child.name(), "w:tbl") == 0){printf("find a w:tbl \n");allElements.push_back(it_child.name());}}printf("the total row = %d\n",(int) allElements.size());return allElements;
}
总结:find_node()参数需要按照官网例子方法自己构造,注意返回值,当前节点所有子节点没有找到这个目标就会返回一个空节点,而不是false