在 C++ 中使用 RapidXML 写入 XML 文件

write to an xml file using rapidxml in c++

本文关键字:写入 XML 文件 RapidXML C++      更新时间:2023-10-16
#include "rapidxml-1.13/rapidxml.hpp"
#include "rapidxml-1.13/rapidxml_print.hpp"
#include "rapidxml-1.13/rapidxml_utils.hpp"
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace rapidxml;
int main()
{
    std::ofstream theFile ("trial.xml");
    xml_document<> doc;
    xml_node<>* decl = doc.allocate_node(node_declaration);
    decl->append_attribute(doc.allocate_attribute("version", "1.0"));
    decl->append_attribute(doc.allocate_attribute("encoding", "UTF-8"));
    doc.append_node(decl);
    xml_node<>* root = doc.allocate_node(node_element, "page");
    root->append_attribute(doc.allocate_attribute("xmlns", "http://ALTEC-Center.org/xsd/ocr-annotation-1-0.xsd"));
    root->append_attribute(doc.allocate_attribute("Number of lines", "10"));
    doc.append_node(root);
    for (int i = 0; i < 8; i++)
    {
        //char  buf1[8];
        //std::sprintf(buf1, "%d", i);
        xml_node<>* child = doc.allocate_node(node_element, "line");
        char * idxStr = doc.allocate_string("gvs");
        child->append_attribute(doc.allocate_attribute("Index",idxStr));
        root->append_node(child);
        for (int j = 0; j < 8; j++)
        {
            xml_node<>* child1 = doc.allocate_node(node_element, "word");
            child1->append_attribute(doc.allocate_attribute("Index","asdvs"));
            child1->append_attribute(doc.allocate_attribute("x","0.0"));
            child1->append_attribute(doc.allocate_attribute("y","0.1"));
            child1->append_attribute(doc.allocate_attribute("width","0.2"));
            child1->append_attribute(doc.allocate_attribute("hight","0.3"));
            child1->append_attribute(doc.allocate_attribute("word","محمد"));
            child->append_node(child1);
        }
    }
    theFile << doc;
    theFile.close();
    doc.clear();
    return 0;
}

/在运行代码时,我收到以下错误:"print_children"未在此范围内声明,并且在实例化时通过依赖于参数的查找找不到声明 [-fpermissive]|/

看起来像是rapidxml 1.13中的一个错误。这是错误条目:

https://sourceforge.net/p/rapidxml/bugs/16/

当它工作GCC时,它在2011中标记为clang++。可能是较新版本的GCC以与过去相同的方式中断clang++

print_node(( 是指尚未定义的其他方法(它们在下面立即定义(。我通过将 print_node(( 的 impl 移到它的兄弟 print_children((、print_element_node(( 等下方来解决这个问题,并在顶部放置一个 print_node(( 的前向 decl 来解决这个问题。附上我的差异。差异中的转速数字来自我的 vcs。

Clang似乎比gcc更挑剔的语言规则,我更喜欢这样,FWIW。

也许GCC和其他编译器现在"对语言规则挑剔"足以发现这个错误?