我的内部循环 xml 在 C++ 向量中没有像预期的那样使用 write_xml
My inner loop xml is not working as expected in C++ vector as expected using write_xml
我尝试使用 vector 中的 write_xml 编写一个 xml 文件。但是我无法获得所需的输出,因为 expected.I 已经给出了示例输入、代码、输出和所需的输出
typedef std::vector<player> playertype; //vector
static playertype playerList;
typedef struct
{
std::string Main; //EX: three val after sorting based on name player1,player2,player2
std::string val1; //EX: three values 100,200,300
std::string val2; // EX: three values 250, 200,250
} player;
if(!playerList.empty())
{
ptree tree1,tree2,tree3,tree4;
playertype::iterator iter;
std::cout<<"\npsdrec match\n";
tree1.put("match","2");
std::string duplicate ="";
std::sort(playerList.begin(), playerList.end(), sortByMain); //this function will sort out based main value in vector array
for(iter = playerList.begin(); iter != playerList.end();iter++)
{
std::cout<<"First duplicate="<<duplicate<<"\t main_name="<<iter->Main<<"\n";
if((duplicate.compare(iter->Main)) != 0)
{
std::cout<<"first compare diff val \n";
tree2.put("main_name",iter->Main);
tree3.put("name","testmatch");
}
tree4.put("tree4",iter->val1);
tree4.put("runs",iter->val2);
duplicate = iter->Main;
++iter;
if(iter == playerList.end() || (duplicate.compare(iter->Main)) != 0) //compare with next value
{
std::cout<<"second or last value \n";
tree3.add_child("value",tree4);
tree2.add_child("play_matchit",tree3);
tree1.add_child("playit",tree2);
}
--iter;
std::cout<<"endof loop \n";
}
pt.add_child("match_list.match_item",tree1);
}
boost::property_tree::xml_writer_settings<char> settings('\t', 1);
write_xml(filename, pt, std::locale(), settings);
我得到了以下输出
> <?xml version="1.0" encoding="utf-8"?> <match_list>
> <match_item>
> <match>2</match>
> <playit>
> <main_name>player1</main_name>
> <play_matchit>
> <name>testmatch</name>
> <value>
> <tree4>100</tree4>
> <runs>250</runs>
> </value>
> </play_matchit>
> </playit>
> <playit>
> <main_name>player2</main_name>
> <play_matchit>
> <name>testmatch</name>
> <value>
> <tree4>100</tree4>
> <runs>250</runs>
> </value>
> </play_matchit>
> <play_matchit>
> <name>testmatch</name>
> <value>
> <tree4>200</tree4>
> <runs>200</runs>
> </value>
> <value>
> <tree4>300</tree4>
> <runs>250</runs>
> </value>
> </play_matchit>
> </playit>
> </match_item> </match_list>
我正在以不正确的方式执行某些操作。你能帮我得到想要的输出吗?
预期输出:
> <?xml version="1.0" encoding="utf-8"?> <match_list>
> <match_item>
> <match>2</match>
> <playit>
> <main_name>player1</main_name>
> <play_matchit>
> <name>testmatch</name>
> <value>
> <tree4>100</tree4>
> <runs>250</runs>
> </value>
> </play_matchit>
> </playit>
> <playit>
> <main_name>player2</main_name>
> <play_matchit>
> <name>testmatch</name>
> <value>
> <tree4>200</tree4>
> <runs>200</runs>
> <tree4>300</tree4>
> <runs>250</runs>
> </value>
> </play_matchit>
> </playit>
> </match_item> </match_list>
"put" 和 "put_child" 不允许重复条目。 "add" 和 "add_child" 允许重复条目。为了获得我想要的输出,我更改了代码如下:-
if(!playerList.empty())
{
ptree tree1,tree2,tree3,tree4;
playertype::iterator iter;
tree1.put("match","2");
std::string duplicate ="";
std::sort(playerList.begin(), playerList.end(), sortByMain); //this function will sort out based main value in vector array
for(iter = playerList.begin(); iter != playerList.end();)
{
std::cout<<"First duplicate="<<duplicate<<"\t main_name="<<iter->Main<<"\n";
if((duplicate.compare(iter->Main)) != 0)
{
std::cout<<"first compare diff val \n";
tree2.put("main_name",iter->Main);
tree3.put("name","testmatch");
}
if((duplicate.compare(iter->Main)) == 0)
{
tree4.add("tree4",iter->val1);
tree4.add("runs",iter->val2);
}
else
{
tree4.put("tree4",iter->val1);
tree4.put("runs",iter->val2);
}
duplicate = iter->Main;
++iter;
if(iter == playerList.end() || (duplicate.compare(iter->Main)) != 0) //compare with next value
{
std::cout<<"second or last value \n";
tree3.put_child("value",tree4);
tree2.put_child("play_matchit",tree3);
tree1.add_child("playit",tree2);
}
std::cout<<"endof loop \n";
}
pt.add_child("match_list.match_item",tree1);
}
boost::property_tree::xml_writer_settings<char> settings('\t', 1);
write_xml(filename, pt, std::locale(), settings);
我尝试使用 vector 中的 write_xml 编写一个 xml 文件。但是我无法获得所需的输出,因为 expected.I 已经给出了示例输入、代码、输出和所需的输出
typedef std::vector<player> playertype; //vector
static playertype playerList;
typedef struct
{
std::string Main; //EX: three val after sorting based on name player1,player2,player2
std::string val1; //EX: three values 100,200,300
std::string val2; // EX: three values 250, 200,250
} player;
if(!playerList.empty())
{
ptree tree1,tree2,tree3,tree4;
playertype::iterator iter;
std::cout<<"\npsdrec match\n";
tree1.put("match","2");
std::string duplicate ="";
std::sort(playerList.begin(), playerList.end(), sortByMain); //this function will sort out based main value in vector array
for(iter = playerList.begin(); iter != playerList.end();iter++)
{
std::cout<<"First duplicate="<<duplicate<<"\t main_name="<<iter->Main<<"\n";
if((duplicate.compare(iter->Main)) != 0)
{
std::cout<<"first compare diff val \n";
tree2.put("main_name",iter->Main);
tree3.put("name","testmatch");
}
tree4.put("tree4",iter->val1);
tree4.put("runs",iter->val2);
duplicate = iter->Main;
++iter;
if(iter == playerList.end() || (duplicate.compare(iter->Main)) != 0) //compare with next value
{
std::cout<<"second or last value \n";
tree3.add_child("value",tree4);
tree2.add_child("play_matchit",tree3);
tree1.add_child("playit",tree2);
}
--iter;
std::cout<<"endof loop \n";
}
pt.add_child("match_list.match_item",tree1);
}
boost::property_tree::xml_writer_settings<char> settings('\t', 1);
write_xml(filename, pt, std::locale(), settings);
我得到了以下输出
> <?xml version="1.0" encoding="utf-8"?> <match_list>
> <match_item>
> <match>2</match>
> <playit>
> <main_name>player1</main_name>
> <play_matchit>
> <name>testmatch</name>
> <value>
> <tree4>100</tree4>
> <runs>250</runs>
> </value>
> </play_matchit>
> </playit>
> <playit>
> <main_name>player2</main_name>
> <play_matchit>
> <name>testmatch</name>
> <value>
> <tree4>100</tree4>
> <runs>250</runs>
> </value>
> </play_matchit>
> <play_matchit>
> <name>testmatch</name>
> <value>
> <tree4>200</tree4>
> <runs>200</runs>
> </value>
> <value>
> <tree4>300</tree4>
> <runs>250</runs>
> </value>
> </play_matchit>
> </playit>
> </match_item> </match_list>
我正在以不正确的方式执行某些操作。你能帮我得到想要的输出吗?
预期输出:
> <?xml version="1.0" encoding="utf-8"?> <match_list>
> <match_item>
> <match>2</match>
> <playit>
> <main_name>player1</main_name>
> <play_matchit>
> <name>testmatch</name>
> <value>
> <tree4>100</tree4>
> <runs>250</runs>
> </value>
> </play_matchit>
> </playit>
> <playit>
> <main_name>player2</main_name>
> <play_matchit>
> <name>testmatch</name>
> <value>
> <tree4>200</tree4>
> <runs>200</runs>
> <tree4>300</tree4>
> <runs>250</runs>
> </value>
> </play_matchit>
> </playit>
> </match_item> </match_list>
"put" 和 "put_child" 不允许重复条目。 "add" 和 "add_child" 允许重复条目。为了获得我想要的输出,我更改了代码如下:-
if(!playerList.empty())
{
ptree tree1,tree2,tree3,tree4;
playertype::iterator iter;
tree1.put("match","2");
std::string duplicate ="";
std::sort(playerList.begin(), playerList.end(), sortByMain); //this function will sort out based main value in vector array
for(iter = playerList.begin(); iter != playerList.end();)
{
std::cout<<"First duplicate="<<duplicate<<"\t main_name="<<iter->Main<<"\n";
if((duplicate.compare(iter->Main)) != 0)
{
std::cout<<"first compare diff val \n";
tree2.put("main_name",iter->Main);
tree3.put("name","testmatch");
}
if((duplicate.compare(iter->Main)) == 0)
{
tree4.add("tree4",iter->val1);
tree4.add("runs",iter->val2);
}
else
{
tree4.put("tree4",iter->val1);
tree4.put("runs",iter->val2);
}
duplicate = iter->Main;
++iter;
if(iter == playerList.end() || (duplicate.compare(iter->Main)) != 0) //compare with next value
{
std::cout<<"second or last value \n";
tree3.put_child("value",tree4);
tree2.put_child("play_matchit",tree3);
tree1.add_child("playit",tree2);
}
std::cout<<"endof loop \n";
}
pt.add_child("match_list.match_item",tree1);
}
boost::property_tree::xml_writer_settings<char> settings('\t', 1);
write_xml(filename, pt, std::locale(), settings);