【问题标题】:how to fill .xml document using rapidxml C++ [duplicate]如何使用 rapidxml C++ 填充 .xml 文档 [重复]
【发布时间】:2014-09-23 09:49:18
【问题描述】:

我正在尝试这样的事情,

#include "stdafx.h"
#include "rapidxml.hpp"
//#include "rapidxml_iterators.hpp"
#include "rapidxml_print.hpp"
#include "rapidxml_utils.hpp"
#include <iostream>
#include <stdio.h>

using namespace rapidxml;

int _tmain(int argc, _TCHAR* argv[])
{

    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");
        child->append_attribute(doc.allocate_attribute("Index",std::to_string(i).c_str()));
        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",std::to_string(j).c_str()));
            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;
}

但是输出如下,

<line Index=" ">
    <word Index=" " x="0.0" y="0.1" width="0.2" hight="0.3" word="محمد"/>
    <word Index=" " x="0.0" y="0.1" width="0.2" hight="0.3" word="محمد"/>
    <word Index=" " x="0.0" y="0.1" width="0.2" hight="0.3" word="محمد"/>
    <word Index=" " x="0.0" y="0.1" width="0.2" hight="0.3" word="محمد"/>
    <word Index=" " x="0.0" y="0.1" width="0.2" hight="0.3" word="محمد"/>
    <word Index=" " x="0.0" y="0.1" width="0.2" hight="0.3" word="محمد"/>
    <word Index=" " x="0.0" y="0.1" width="0.2" hight="0.3" word="محمد"/>
    <word Index=" " x="0.0" y="0.1" width="0.2" hight="0.3" word="محمد"/>
</line>

我想要“index”属性=循环“i”和“j”的迭代器值

<word Index="0" x="0.0" y="0.1" width="0.2" hight ="0.3" word="محمد"/> //j=0
<word Index="1" x="0.0" y="0.1" width="0.2" hight="0.3" word="محمد"/>  //j=1
<word Index="2" x="0.0" y="0.1" width="0.2" hight="0.3" word="محمد"/>
<word Index="3" x="0.0" y="0.1" width="0.2" hight="0.3" word="محمد"/>

等等。 在前面的示例中,我只是尝试按原样使用循环参数,但后来我想用word[i][j] 之类的东西填充属性,我该怎么做?

【问题讨论】:

    标签: c++ rapidxml


    【解决方案1】:

    您遇到了最常见的 rapidxml 问题之一:当您以这种方式构建 XML 文档时,RapidXML 不会复制您提供的字符串数据,它只是存储指向它们的指针。 (这是使其快速的重要部分)。

    所以当你使用这个

    child->append_attribute(doc.allocate_attribute("Index",std::to_string(i).c_str()));
    

    .. 它存储了一个指向从to_string(i) 返回的临时字符串的指针。此内存将被覆盖,因此生成的 XML 文档将发生变化。

    相反,您应该分配一个临时的 rapidXml 字符串来保存文本:

    char * idxStr = doc.allocate_string(std::to_string(i).c_str()); 
    child->append_attribute(doc.allocate_attribute("Index", idxStr));
    

    除非您使用文字字符串(例如"0.3"),否则您应该始终这样做

    【讨论】:

      猜你喜欢
      • 2013-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-15
      • 2012-11-06
      • 2013-02-22
      • 2013-02-20
      • 2017-11-03
      相关资源
      最近更新 更多