Serialise a Model into CellML 2.0 for printing to a fileΒΆ

The Printer class has the opposite functionality to the Parser class. It accepts an existing ModelPtr and creates a string which can be written to a file. As with the Parser and the Validator, the Printer also records a collection of Issue items which can be accessed as shown below.

// Create a Printer instance.
auto printer = libcellml::Printer::create();

// The output of the printModel function is a string representing the serialised input model.
std::string serialisedModelString = printer->printModel(model);

// Check the printer for issues.
assert(printer->issueCount() == 0);

// Write the serialised string to a file.
std::string outFileName = "my_printed_file.cellml";
std::ofstream outFile(outFileName);
outFile << serialisedModelString;
outFile.close();

Details of any issues that the printer encounters and records can be retrieved as described on the Retrieve Issue items page.