如何使用rapidxml C++填充.xml文档
how to fill .xml document using rapidxml C++
我正在尝试这样的东西,
#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"answers"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]
之类的东西填充属性,我该怎么做呢?
您遇到了一个最常见的rapidxml问题:当您以这种方式构建XML文档时,rapidxml不会复制您提供的字符串数据,它只存储指向它们的指针。(这是Rapid的重要组成部分)。
所以当你使用这个
child->append_attribute(doc.allocate_attribute("Index",std::to_string(i).c_str()));
它存储指向从CCD_ 2返回的临时字符串的指针。此内存将被覆盖,因此生成的XML文档将发生更改。
相反,您应该分配一个临时的rapidXml字符串来保存文本:
char * idxStr = doc.allocate_string(std::to_string(i).c_str());
child->append_attribute(doc.allocate_attribute("Index", idxStr));
除非使用文字字符串(例如"0.3"
)
相关文章:
- 解析XML文档
- 使用RapidXML遍历一个简单的DOM Xml文档
- 如何从“std::istream”加载XML文档
- 需要在c++中为xml文档提供一个通过xpath导航的算法
- 有没有办法修改样式表,以便它将带有空标记的 XML 文档转换为 <tag />?
- 使用 TinyXML 在 XML 文档中查找特定节点
- 我正在使用 Xercesc 来解析 xml 文档。我想知道我将如何使用 XML 值作为程序的输入?
- 无法使用沙堡 (SHFB) 生成 XML 文档帮助文件
- 如何使用 QDomDocument 从 XML 文档中删除嵌套标记
- xml_node<wchar_t> 我正在使用快速 xml 将 XML 文档打印到文件
- 重新排列大型XML文档
- 如何使用rapidxml C++填充.xml文档
- 如何在c++中解析XML文档
- 如何编辑结束的XML文档结束在Qt (writeEndDocument)
- 如何在任何时候停止使用libxmlsax解析xml文档
- 如何根据libxml++中的relaxNG模式验证xml文档
- 向xml文档添加xml文本结构
- POCO C++SAX解析器:如果xml文档编码是ANSI,那么下一条语句不是读取并抛出编码错误异常
- 在经过预处理的svg-xml文档上设置QGraphicsSvgItem的呈现器非常慢
- 有谁知道C++类似于 的 XML 解析器/编写器。Net的XML文档?