Attributes in property tree are added as sub-nodes.
property tree 属性作为子节点 (坑坑坑坑坑)
使用
write_xml_element(std::cout, pt.front().first, pt.front().second, 0, boost::property_tree::xml_writer_make_settings<std::string>(' ', 0));
1 You can use the undocumented function write_xml_element to write the single element (using the key as element name): // write the single element: (undocumented API) boost::property_tree::xml_parser::write_xml_element( std::cout, it->first, it->second, 0, settings ); 2 or you can create a new ptree object with the single child ptree tmp; tmp.add_child(it->first, it->second); write_xml(std::cout, tmp, settings);
#include <iostream> #include <boost/property_tree/xml_parser.hpp> #include <boost/property_tree/ptree.hpp> using namespace std; int main() { auto str=R"(xmlcontent)"; using namespace boost::property_tree; iptree tree; stringstream ss; ss<<str; boost::property_tree::xml_parser::read_xml(ss,tree); iptree pchilds = tree.get_child("MainTask"); // write_xml(std::cout, tree); for (auto pos= pchilds.begin(); pos != pchilds.end(); ++pos) { bool decode_flag = false; std::string name = pos->first; iptree tempNode = pos->second; if (name == "<xmlattr>") { continue; } if (name == "subnode") //子节点 { //获取所有子节点
iptree temppp;
auto& attributes = tempNode.get_child("<xmlattr>", temppp);
for(auto &v: attributes){
cout << "First: " << v.first.data() << " Second: " << v.second.data() << endl;
}
for(auto child:tempNode.get_child("")){ //if( child.first == "<xmlattr>" ) { // write_xml(std::cout,child.second); // write_xml_element(std::cout, child.first, child.second, 0, boost::property_tree::xml_writer_make_settings<std::string>(' ', 0)); write_xml_element(std::cout,ptree::key_type(),child.second,0,xml_writer_make_settings<std::string>('', 0)); //cout<<child.second.data()<<endl; // } } //string sss=tempNode.get("<xmlattr>", ""); //std::cout<<tempNode.data()<<endl; //write_xml(std::cout,tempNode ,options); } } }
参考:
https://www.titanwolf.org/Network/q/d9d0c6a1-6771-4da9-985e-a7975028ebee/y
https://stackoverflow.com/questions/14010473/parsing-xml-attributes-with-boost
https://www.technical-recipes.com/2014/using-boostproperty_tree/
使用
http://www.ce.unipr.it/people/medici/boost_ptree.html
https://topic.alibabacloud.com/a/boost-ptree-operation-xml-easy-and-useful_8_8_10266710.html
关键点
https://stackoverflow.com/questions/53603392/boost-property-tree-iterating-through-attributes-of-repeated-child-elements-wit
https://stackoverflow.com/questions/16058204/omit-xml-declaration-when-saving-xml-with-boost
忽略声明
https://stackoverflow.com/questions/47448864/removing-encoding-attribute-from-xml-using-boost
https://stackoverflow.com/questions/16058204/omit-xml-declaration-when-saving-xml-with-boost
格式化
https://stackoverflow.com/questions/55980786/how-to-serialize-a-subtree-of-a-boost-property-tree-to-xml
https://stackoverflow.com/questions/35954432/how-to-get-all-child-node-values