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");
source share