2 Single Object as XML

2.1 Serialization Description File

The serialization description file contains the class with data to be serialized:

        // book.xmlser:

        enum Color
        {
            black, white, blue
        }

        class Book
        {
            string name;
            string isbn;
            string author;
            int published;
            string publisher;
            float price;
            Color color;
        }
	

I have used extensions .xmlser and .xml-schema for these kind of files.

2.2 Running xmlsergen

The next step is to run the xmlsergen tool with serialization description file as input:

        >xmlsergen -v book.xmlser
        > book.xmlser
        ==> book.hpp
        ==> book.cpp
    

The xmlsergen tool generates XML serializable C++ classes from classes in the serialization description file and puts their definitions to the generated .cpp and .hpp files.

2.3 Creating Main Program

Because the XML serialization library uses C++ concepts that are recently added to the C++ standard, the program should be compiled with /std:c++20.

The main program:

        // object: main.cpp:

        #include <book.hpp>
        #include <sngxml/dom/Document.hpp>
        #include <sngxml/dom/Parser.hpp>
        #include <sngxml/xpath/InitDone.hpp>
        #include <soulng/util/InitDone.hpp>
        #include <soulng/util/Unicode.hpp>
        #include <iostream>

        void InitApplication()
        {
            soulng::util::Init();
            sngxml::xpath::Init();
        }

        void DoneApplication()
        {
            sngxml::xpath::Done();
            soulng::util::Done();
        }

        int main()
        {
            try
            {
                InitApplication();

                Book book;
                book.name = "The C++ Programming Language, 4th Edition";
                book.isbn = "0-321-56384-0";
                book.author = "Bjarne Stroustrup";
                book.published = 2013;
                book.publisher = "Pearson Education";
                book.price = 61.88f;
                book.color = Color::blue;

                std::unique_ptr<sngxml::dom::Element> element = book.ToXml("book");
                sngxml::dom::Document doc;
                doc.AppendChild(std::unique_ptr<sngxml::dom::Node>(element.release()));
                std::stringstream strStream;
                soulng::util::CodeFormatter formatter(strStream);
                doc.Write(formatter);
                std::string str = strStream.str();
                std::cout << str << std::endl;

                std::u32string content = soulng::unicode::ToUtf32(str);
                std::unique_ptr<sngxml::dom::Document> docRead = sngxml::dom::ParseDocument(content, "string");

                Book bookRead;
                bookRead.FromXml(docRead->DocumentElement());
                std::cout << bookRead.name << std::endl;
            }
            catch (const std::exception& ex)
            {
                std::cerr << ex.what() << std::endl;
                return 1;
            }
            DoneApplication();
            return 0;
        }    
    

2.4 Program Output

The output of the program looks like this:

        <book classId="-1" className="Book" objectId="00000000-0000-0000-0000-000000000000">
            <name value="The C++ Programming Language, 4th Edition"/>
            <isbn value="0-321-56384-0"/>
            <author value="Bjarne Stroustrup"/>
            <published value="2013"/>
            <publisher value="Pearson Education"/>
            <price value="61.880001"/>
            <color value="2"/>
        </book>	

        The C++ Programming Language, 4th Edition