Boost logo

Boost Users :

Subject: [Boost-users] Boost PropertyTree: read_json() followed by write_xml produces invalid XML
From: Marcel Loose (loose_at_[hidden])
Date: 2014-04-26 17:39:43

Hi all,

I was quite surprised and disappointed to find out that I was unable
to convert a simple JSON file into XML, using the Boost PropertyTree.
As input I used the example JSON file from the documentation (see
example.json below).

I wrote a small program (see below) that reads in this JSON
file, writes it to disk as XML file, and finally tries to read back
this XML file. This last operation fails with an exception (see output
below); the XML file that is produced (see example.xml below) is
invalid, because it contains empty tags (<>).

Am I doing something wrong?

Best regards,
Marcel Loose.

$ cat example.json
      "foo": true,
      "bar": "true",
      "value": 102.3E+06,
         {"value": "New", "onclick": "CreateNewDoc()"},
         {"value": "Open", "onclick": "OpenDoc()"}

$ cat
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <fstream>

using namespace std;
using namespace boost::property_tree;

int main()
  ptree pt;
    read_json("example.json", pt);
    ofstream ofs("example.xml");
    write_xml(ofs, pt, xml_writer_make_settings(' ', 2));
    ifstream ifs("example.xml");
    read_xml(ifs, pt);
  return 0;

$ ./example
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::property_tree::xml_parser::xml_parser_error> >'
  what(): <unspecified file>(7): expected element name

$ cat example.xml
<?xml version="1.0" encoding="utf-8"?>

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at