2013-04-21 146 views
6

我正在使用Boost的屬性樹來讀取和寫入XML。使用我製作的電子表格應用程序,我想將電子表格的內容保存爲xml。這是學校裏的功課,所以我需要使用以下格式爲XML:將具有相同鍵的節點添加到屬性樹

<?xml version="1.0" encoding="UTF-8"?> 
<spreadsheet> 
    <cell> 
     <name>A2</name> 
     <contents>adsf</contents> 
    </cell> 
    <cell> 
     <name>D6</name> 
     <contents>345</contents> 
    </cell> 
    <cell> 
     <name>D2</name> 
     <contents>=d6</contents> 
    </cell> 
</spreadsheet> 

對於一個簡單的測試程序中,我寫道:在此基礎上question我看到put方法取代

int main(int argc, char const *argv[]) 
{ 
boost::property_tree::ptree pt; 

pt.put("spreadsheet.cell.name", "a2"); 
pt.put("spreadsheet.cell.contents", "adsf"); 

write_xml("output.xml", pt); 

boost::property_tree::ptree ptr; 
read_xml("output.xml", ptr); 

ptr.put("spreadsheet.cell.name", "d6"); 
ptr.put("spreadsheet.cell.contents", "345"); 
ptr.put("spreadsheet.cell.name", "d2"); 
ptr.put("spreadsheet.cell.contents", "=d6"); 

write_xml("output2.xml", ptr); 

return 0; 
} 

該節點上的任何內容,而不是添加新節點。這也正是我所看到的功能:

的Output.xml

<?xml version="1.0" encoding="utf-8"?> 
<spreadsheet> 
    <cell> 
    <name>a2</name> 
    <contents>adsf</contents> 
    </cell> 
</spreadsheet> 

Output2.xml

<?xml version="1.0" encoding="utf-8"?> 
<spreadsheet> 
    <cell> 
    <name>d2</name> 
    <contents>=d6</contents> 
    </cell> 
</spreadsheet> 

望着documentation我看到這個add_child方法,它將Add the node at the given path. Create any missing parents. If there already is a node at the path, add another one with the same key.

我不太明白如何使用add_child方法,有人可以解釋如何使用它嗎?

有沒有更好的方法去實現我想要的文件格式呢?

+0

你就不能在孩子的名字使用單元格的名字嗎?即''spreadsheet.cell.d6「' – 2013-04-21 22:55:44

+0

@ k-ballo,因爲那不符合xml要求。 – Deekor 2013-04-21 23:03:04

回答

15

add_child成員函數允許您將一個property_tree作爲子節點插入到另一個的DOM中。如果您提供的關鍵路徑已經存在,則會添加重複的關鍵字,並且該子項將被插入。如果我們稍微改變你的例子,我們可以檢查結果。

#include <boost/property_tree/ptree.hpp> 
#include <boost/property_tree/xml_parser.hpp> 

int main() 
{ 
    // Create the first tree with two elements, name and contents 
    boost::property_tree::ptree ptr1; 
    ptr1.put("name", "a2"); 
    ptr1.put("contents", "adsf"); 

    // Create the a second tree with two elements, name and contents 
    boost::property_tree::ptree ptr2; 
    ptr2.put("name", "d6"); 
    ptr2.put("contents", "345"); 

    // Add both trees to a third and place them in node "spreadsheet.cell" 
    boost::property_tree::ptree ptr3; 
    ptr3.add_child("spreadsheet.cell", ptr1); 
    ptr3.add_child("spreadsheet.cell", ptr2); 

    boost::property_tree::write_xml("output.xml", ptr3); 

    return 0; 
} 

當你調用add_child在第一時間,對鍵「spreadsheet.cell」節點不存在,被創建。然後它將樹的內容(namecontents)添加到新創建的節點。當您第二次看到「spreadsheet.cell」存在時會調用add_child,但與put不同,它將創建一個也稱爲「單元格」的同級節點,並將其插入到同一位置。

最終輸出:

<?xml version="1.0" encoding="utf-8"?> 
<spreadsheet> 
    <cell> 
    <name>a2</name> 
    <contents>adsf</contents> 
    </cell> 
    <cell> 
    <name>d6</name> 
    <contents>345</contents> 
    </cell> 
</spreadsheet> 
+0

真棒解釋。如果我想更新單元格「d6」,但是,我將如何導航到該單元格並更改「ptr3」中的內容? – Deekor 2013-04-21 22:59:29

+2

@Deekor遍歷「電子表格」的子元素,查找所有「cell」類型的子元素。任何時候你遇到一個獲得'name'的內容,如果你找到一個匹配項,就刪除它。你需要看文檔的具體內容 – 2013-04-21 23:03:12

+0

很好的解釋,否則沒有太多關於add_child的文檔。 – sb32134 2016-01-21 10:01:02

相關問題