使用c ++中的rapidxml写入xml文件

时间:2018-01-05 05:40:11

标签: c++ xml rapidxml

#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] | /

1 个答案:

答案 0 :(得分:0)

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

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

clang++适用于2011时,GCC标记为GCC。可能是clang++的较新版本以GCC过去相同的方式中断:

  

print_node()指的是尚未定义的其他方法(它们在下面定义)。我通过将print_node()的impl向下移动到其brethren print_children(),print_element_node()等下面来解决这个问题,并将print_node()的前向decl放在顶部。我的差异附加。差异中的转数来自我的vcs。

     

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

或许{{1}}和其他编译器现在对语言规则挑剔了#34;足以拿到这个bug?