Writing harder than trivial xml with boost property tree

UPD I found the answer to the question "formatting" here , so I delete this part of the question, please read the updated question:

I need to write xml to a C ++ file system. I recognized this titorial. The tutorial uses pretty simple xml. My xml is more complex and I don't know how to change the code to create it. Here is what I code:

#include <boost/property_tree/ptree.hpp> #include <boost/property_tree/xml_parser.hpp> //<Root> // <Set Name="1"> // <Field Name="Hello 1"/> // <Field Name="World 1"/> // </Set> // <Set Name="2"> // <Field Name="Hello 2"/> // <Field Name="World 2"/> // </Set> //</Root> int main(int argc, char* argv[]) { using boost::property_tree::ptree; ptree pt; pt.put("Root.Set.Field", "Hello"); pt.put("Root.Set.Field", "World"); boost::property_tree::xml_writer_settings<char> settings('\t', 1); write_xml("testXml.xml", pt, std::locale(), settings); return 0; } 

Output:

 <?xml version="1.0" encoding="utf-8"?> <Root> <Set> <Field>World</Field> </Set> </Root> 

How can I change my program to create the desired xml, in particular:

  • How to add a few nods with the same name? Adding true like this pt.put("Root.Set.Field", "Hello", true); is a compile-time error
  • How to set xml attributes? ( Name="Hello 1" ) According to the document It seems I should add them to the "subkeys", but how?

upd I tried: pt.put("Root.Set.Field.xmlattr.Name", "Hello 1"); expecting to see that <Field Name="Hello 1"/> , but still doesn't work. Waiting for someone to use the correct syntax.

upd2 bingo, this syntax works, I will continue to try to print the desired xml tomorrow. pt.put("Root.Set.Field.<xmlattr>.Name", "Hello 1");

+6
source share
2 answers

This answers the last question - how to use multiple nodes with the same name. Finally, I wrote a program that solves the problem

 #include <boost/property_tree/ptree.hpp> #include <boost/property_tree/xml_parser.hpp> //<Root> // <Set Name="1"> // <Field Name="Hello 1"/> // <Field Name="World 1"/> // </Set> // <Set Name="2"> // <Field Name="Hello 2"/> // <Field Name="World 2"/> // </Set> //</Root> int main(int argc, char* argv[]) { using boost::property_tree::ptree; ptree pt; boost::property_tree::ptree rootNode; boost::property_tree::ptree setNode1; boost::property_tree::ptree setNode2; boost::property_tree::ptree fieldNode1; boost::property_tree::ptree fieldNode2; boost::property_tree::ptree fieldNode3; boost::property_tree::ptree fieldNode4; fieldNode1.put("<xmlattr>.Name", "Hello 1"); fieldNode2.put("<xmlattr>.Name", "World 1"); fieldNode3.put("<xmlattr>.Name", "Hello 2"); fieldNode4.put("<xmlattr>.Name", "World 2"); setNode1.add_child("Field", fieldNode1); setNode1.add_child("Field", fieldNode2); setNode2.add_child("Field", fieldNode3); setNode2.add_child("Field", fieldNode4); setNode1.put("<xmlattr>.Name", "1"); setNode2.put("<xmlattr>.Name", "2"); rootNode.add_child("Set", setNode1); rootNode.add_child("Set", setNode2); pt.add_child("Root", rootNode); boost::property_tree::xml_writer_settings<char> settings('\t', 1); write_xml("testXml.xml", pt, std::locale(), settings); return 0; } 

Output:

 <?xml version="1.0" encoding="utf-8"?> <Root> <Set Name="1"> <Field Name="Hello 1"/> <Field Name="World 1"/> </Set> <Set Name="2"> <Field Name="Hello 2"/> <Field Name="World 2"/> </Set> </Root> 
+10
source

property_tree is not intended as a generic XML API. It is intended for application configuration settings. Think: Windows INI files or C # .config files or Java.properties files. If you try to treat it as an XML parser, you will be unhappy.

What you should use depends on your requirements. xerces , for example, is an enterprise-class library. You can use proprty_tree if you can be more flexible in the data structure. There are many other options available.

+1
source

Source: https://habr.com/ru/post/954077/


All Articles