Quick /

opC++ Features

opC++ Compiler

opC++ Language

opGames Company

Rss

Rss Serialization Tutorial

In this quick tutorial we show how easy it is to load existing xml data into C++ via opC++ serialization.

Suppose that you want to load an xml file in the rss format:

In this rss file, we have an outer rss tag, and an inner channel tag. The easiest way to load xml data into opC++ is to declare opclasses and data members that match the xml.

RSS Files: we need to match these xml tags to load rss

  • <rss>
  • <item>
  • <channel>
  • any other tags

An opclass is a category introduced by the opC++ Standard Dialect. It is a step above the C++ class construct, and automatically adds serialization and reflection features.

Let's create three opclasses that match the xml: Item, Channel, and Rss. We first declare the opclass to represent an <item>:

An opclass looks just like a class and has private visibility. Notice that all of Item's data members match the names of the rss item's tags. Here, they are all strings, but they may be any basic type, opclass, opstruct, openum, or standard stl container of these types. Even pointers to opstructs and opclasses are supported. Now let's declare an opclass to represent a <channel>:

Our Channel opclass is nearly the same as the Item opclass except that its data members are different. Also, notice that each channel in the rss file has a list of items. We can declare this an stl vector of Item opclasses, as opC++ serialization supports stl containers of opclasses.

Here we named the container Items, which does not match the rss tag, <item>. To fix this, we can use one of the opC++ Standard Dialect's valued data modifiers, opreflect. opreflect can be used to specify a variable's reflection name. Since serialization uses reflection, setting this valued data modifier to "item" will cause the list of items to be loaded correctly. Finally, we need to declare our Rss opclass:

The Rss opclass is similar to the Channel opclass since it has to store a list of opclasses again, channels in this case. Now, all that remains is to use one of opC++'s xml archivers on an Rss instance:

We simply open the Rss.xml file using a C++ standard file input stream and pass it to the archiver. After load is called, the Rss object completely mirrors the xml file.

Here is a shot of the successfully loaded object in the Microsoft Visual Studio debugger:

And that's it!

That's how easy serialization is with opC++. Given an xml file in the rss format, we managed to load the entire file into a data structure by writing three small, simple opclasses. The serialization was automatic and seamless.

You can even write your own archivers if the provided text, binary, and xml archivers are not enough. You can also restrict which data members are serialized and reflected via the opC++ Standard Dialect modifiers native and transient.

Project Files (VS 2005): RssSerialization.zip

Recent Changes (All) | Edit SideBar Page last modified on April 06, 2008, at 03:06 PM Edit Page | Page History
Copyright 2010 opGames Inc.