2013-04-21 15 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> 

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; 
} 

をこのquestionに基づいて、私はput方法が代わる見ます新しいノードを追加する代わりに、そのノードにあるものまさに私が見ていた機能である:

の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> 

私はこのadd_childメソッドを参照してくださいdocumentationを見ているだろう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.

私はその方法を使用する方法を考え出すことができません、誰かがそれを使用する方法を説明することができますか?

私が望むファイル形式を実現するためにこれについてもっと良い方法がありますか?

+0

は、あなただけの子の名前でセル名を使用することができませんか?つまり、「spreadsheet.cell.d6」「 –

+0

@ k-ballo」はXMLの要件を満たしていないためです。 – Deekor

答えて

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」と作成されます。次に、ツリーの内容(nameおよびcontents)を新しく作成したノードに追加します。 add_childを呼び出すと、「spreadsheet.cell」は既に存在すると認識されますが、putとは異なり、「cell」とも呼ばれる兄弟ノードが作成され、同じ場所に挿入されます。

最終出力

<?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

+2

@Deekor 'spreadsheet'の子を繰り返して、' cell'型のすべての子を探します。いつでもあなたは 'name'の内容を取得し、一致するものがあればそれを削除します。具体的には –

+0

のドキュメントを参照する必要があります。そうでない場合は、netのadd_childに関するドキュメントはあまりありません。 – sb32134

関連する問題