2014-09-23 48 views
0

我想這樣的事情來填補.XML文件,如何使用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> 

,我想「索引」屬性=迭代器循環「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]的東西填充屬性,我怎麼能這樣做?

回答

0

您正在絆倒一個最常見的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"