2012-04-22 12 views
1

XMLファイルに一連の結果を書いています。各結果セットには一連の結果が含まれています。私の問題は、(コードのいくつかの実行中に)いくつかの新しい結果をセットに書き込もうとすると、XMLパーサーは既存のXMLファイルの最初の(上の)結果セットを取得し、その結果を(古い)セット。例:子ノードをXML構造の最上位に追加します。ブーストプロパティツリー

<root> 
    <result_set result_number="0"> <--- Parser selects this result set 
    <result number="0"> 
     <tolerance>100</tolerance> 
    </result> 
    <result number="1"> 
     <tolerance>100</tolerance> 
    </result> 
    <resultnumber="0">   <---- This should be added to result set 1  
     <tolerance>100</tolerance> 
    </result> 
    </result_set> 
    <result_set result_number="1"/> <-- New result set added to Xml, missing results 
</root> 

したがって、最新の結果セットをXMLファイルの先頭に追加することは可能ですか?または、最新の結果セットを取得するときに、リストの最後のものをフェッチしますか?

私は自分自身をはっきりと説明してくれることを願っています。 おかげ

(私が使用しているサンプルコード)

void initialise(std::string filename) 
{ 
    ptree pt; 
    xml_writer_settings<char> w('\t', 1); 
    read_xml(filename, pt, boost::property_tree::xml_parser::trim_whitespace); 
    std::ofstream xmlFile(filename.c_str(), std::ios::out); 

    // Probably not the best way to check for a root node 
    try 
    { 
    ptree & rootNode = pt.get_child("root"); 
    } 
    catch(...) 
    { 
    xmlFile << "<root></root>" << std::endl; 
    read_xml(filename, pt, boost::property_tree::xml_parser::trim_whitespace); 
    } 


    ptree & rootNode = pt.get_child("root"); 
    ptree resultSetNode; 
    resultSetNode.add("<xmlattr>.result_number", 0); 
    rootNode.add_child("result_set", resultSetNode); 
    write_xml(filename, pt, std::locale(), w); 

} 

void save1(std::string filename) 
{ 
    ptree pt; 
    xml_writer_settings<char> w('\t', 1); 
    read_xml(filename, pt, boost::property_tree::xml_parser::trim_whitespace); 

    ptree &resultSetNode = pt.get_child("root.result_set"); 
    ptree resultNode; 
    resultNode.put("tolerance", 100); 
    resultSetNode.add_child("result", resultNode); 

    write_xml(filename, pt, std::locale(), w); 

} 

int main() 
{ 
    initialise("sample.xml"); 

    for(int i = 0; i < 2; ++i) 
    { 
    save1("sample.xml"); 
    } 
    std::cout << "Success!!!\n"; 
    return 0; 
} 

答えて

0

これは、ブーストメーリングリストの私の回答のコピー/ペーストである:

私はあなたがここに全部を誤解していると思います。

あなたはノードのXPathルート/ result_setあるの多くを持っていますが、 の全ては、それらは、それらを特定の属性があります。result_number

あなたは(ブースト ドキュメントからの)任意のノードを取得しているget_child呼び出し:

self_type & get_child(const path_type & path) ; 
    Get the child at the given path, or throw ptree_bad_path. 

    Notes: 
    Depending on the path, the result at each level may not be 
    completely determinate, i.e. if the same key appears 
    multiple times, which child is chosen is not specified. 
    This can lead to the path not being resolved even though 
    there is a  descendant with this path. Example: 
    a -> b -> c 
     -> b 
The path "a.b.c" will succeed if the resolution of "b" chooses the 

最初のノードですが、2番目のノードを選択すると失敗します。私はあなたが最後に実行さを見つけるために、ルート上のイテレータを使用する必要がありますね

は を設定し、それに結果を追加し、そのノードへの参照を取得し、:

from begin() to end(), find the max result_number or 
    just the one that matches count() 

リンクDOC後押しする:http://www.boost.org/doc/libs/1_41_0/doc/html/boost/property_tree/basic_ptree.html#id973373-bb

関連する問題