c++xmlboostboost-propertytree

Why boost property tree xml serializer cannot preserve multi-line values?


I am working with boost property tree (v.1.72.5) to read and write xml files. I know that according to the documentation:

The XML storage encoding does not round-trip perfectly. A read-write cycle loses trimmed whitespace, low-level formatting information, and the distinction between normal data and CDATA nodes. Comments are only preserved when enabled. A write-read cycle loses trimmed whitespace; that is, if the origin tree has string data that starts or ends with whitespace, that whitespace is lost.

According to this known behavior, if I read/write an xml file without using the boost::property_tree::xml_parser::trim_whitespace option, my xml file can be like this:

  <MyInfo id="info_1" title="" description="" >
     
     
     
     234
     <My_Nums>
        <My_Num>0</My_Num>
        <My_Num>1</My_Num>
     </My_Nums>
  </MyInfo>

In this case, reading the value of MyInfo (i.e. 234) is failing. But if I use the boost::property_tree::xml_parser::trim_whitespace option, I can read the value properly (i.e. 234), but all of my multi-line strings are converted into single line values (line breaks are removed.)

How I can read the value of MyInfo tag properly and at the same time preserve my multi line values?


Solution

  • As @sehe has mentioned in the comments, it is how the boost property tree has been implemented. I think the best workaround to avoid this problem is to always use the boost::property_tree::xml_parser::trim_whitespace attribute and also assign the values to an specific tag. With this apprach, my xml file would always be like that:

      <MyInfo id="info_1" title="" description="" >
         <value>234</value>
         <My_Nums>
            <My_Num>0</My_Num>
            <My_Num>1</My_Num>
         </My_Nums>
      </MyInfo>