473,320 Members | 2,041 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Serialize classes using XML

Hi All,

There is a library for Java called XStream which will serialize
instances to XML. It's very easy to use and does not require changing
existing code. Basically, you just call 1 API method and bingo, you
have XML. I was wondering if there is something similar for C++. I have
already investigated the boost serialization library, but it requires
adding a serialize() function to your classes. Does anyone know of an
open source library that is similar to XStream, but for C++? Google did
not turn up anything.
many thanks!

Barry

Jul 23 '05 #1
7 2008
ti**********@hotmail.com wrote:
There is a library for Java called XStream which will serialize
instances to XML. It's very easy to use and does not require changing
existing code. Basically, you just call 1 API method and bingo, you
have XML. I was wondering if there is something similar for C++. [..]


Nope. C++ has no "reflection" API. Information about class members
and class layouts doesn't survive the compilation and linking unless
you specifically represent your objects by writing additional code to
generate your XML or make your objects out of XML.
Jul 23 '05 #2
titanandrews wrote:
There is a library for Java called XStream which will serialize
instances to XML. It's very easy to use and does not require changing
existing code. Basically, you just call 1 API method and bingo, you
have XML. I was wondering if there is something similar for C++. I have
already investigated the boost serialization library, but it requires
adding a serialize() function to your classes. Does anyone know of an
open source library that is similar to XStream, but for C++? Google did
not turn up anything.


You are asking for "reflection", which C++ does not support.

Now ask yourself: What's the value of reflecting _everything_, when the only
part of your application that _should_ serialize is a tiny bit of data?

So, how could the incredible burden of adding serialize() to the few classes
that should persist be too much?

--
Phlip
http://www.c2.com/cgi/wiki?ZeekLand
Jul 23 '05 #3
"Now ask yourself: What's the value of reflecting _everything_, when
the only
part of your application that _should_ serialize is a tiny bit of data?
"

Yep, you're right. The problem is I am dealing with legacy code, and I
didn't want to have to try and figure out exactly what needs to be
serialized and what doesn't. The class I need to serialize contains
many other classes, and these classes may be derived from other
classes, and on and on. So it could become a cumbersome task to
implement serialization. I need to investigate further. I just thought
that there could be any easier way to do this, but you are right, no
reflection in C++. That is what XStream relies on. Didn't think about
it. I guess I was thinking it was magic. ;)

Thanks for the replies!

-B

Jul 23 '05 #4
titanandrews wrote:
Yep, you're right. The problem is I am dealing with legacy code, and I
didn't want to have to try and figure out exactly what needs to be
serialized and what doesn't. The class I need to serialize contains
many other classes, and these classes may be derived from other
classes, and on and on. So it could become a cumbersome task to
implement serialization. I need to investigate further.


Ask your customer, "When you turn the program off, and turn it on again,
what do you want to come back?"

The users want a specific experience. If hardcore serialization were
available, it would be the simplest way to get that experience. Without it,
learn more about what experience they need. It could be just the window
position and the current document.

--
Phlip
http://www.c2.com/cgi/wiki?ZeekLand
Jul 23 '05 #5
In this case, it's a diagram. So it's quite complicated.

Jul 23 '05 #6
titanandrews wrote:
In this case, it's a diagram. So it's quite complicated.


Eeeek. You mean you inherited code that draws a complicated diagram in
memory, without saving it to a file?

Write a unit test that draws the simplest possible diagram in memory. Then
the test calls a Save("scratch.xml") method (with nothing inside it), and
checks that "scratch.xml" now contains the simplest possible representation
of that diagram.

Make the test pass by writing something inside Save(). Write the simplest
possible thing to get the test to pass.

Now write a unit test that loads a fixed XML file, such as
"load_test_1.xml", and draws the diagram in memory. The test will assert
that Load() generates this diagram in memory.

Now write a unit test draws the simplest possible diagram in memory, and
adds one little feature, like a single child item. The test will now assert
that "scratch.xml" contains a representation of this one little feature.
Make the test pass by upgrading Save().

Repeat for the Load method (copying the new "scratch.xml" to
"load_test_2.xml").

Keep going, for each feature, until you have just enough code in Save() and
Load() to save and load only the features in the diagram that are important.

This situation is much more accessible than barfing everything in the
diagram into an XML file. You won't know what you can change, or what's
important. Tests lock all that down.

--
Phlip
http://www.c2.com/cgi/wiki?ZeekLand
Jul 23 '05 #7
No, it actually does save it to a binary file. So I suppose I could use
the saving mechanism currently in place to figure out everything that
needs serializing!

Great! Thanks for your input!

-Barry

Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: grwalker | last post by:
I have some classes that have the <Serializable()> attribute applied, which of course by default serializes the class properties as elements. What I would like to do is to be able to override this...
14
by: vince | last post by:
Can I add (append) to an xml file that already contains a serialized object, and be able to deserialize to either or both objects from the same file...??? How is this done...?? thanks, vince
5
by: David Sworder | last post by:
Hi, I've created a UserControl-derived class called MyUserControl that is able to persist and subsequently reload its state. It exposes two methods as follows: public void Serialize(Stream...
2
by: films | last post by:
I understand the concept. Serialization of a class will add all the sub-objects of the class to the stream if there are also serializible. So say I have: class Author {
7
by: Ben Amada | last post by:
I've created a class that I need to store in ViewState. However when I try to store it in ViewState, I get the following error: "The type 'solution.pe2' must be marked as Serializable or have a...
1
by: Samuel R. Neff | last post by:
We're using XmlSerializer to serialize our data objects for persistence in a WinForms app. This is our primary data store, we're not web connected and are not using a database. We've had to...
1
by: js | last post by:
Does anybody knows how to solve the problem? I added attribute to the following classes in Microsoft.Practices.EnterpriseLibrary.Data namespace, but I still get the error. Thanks. ...
8
by: cd~ | last post by:
I can provide a test app, the news server won't allow me to post the files because they are too large (93KB and 1.2KB) I downloaded the ESRI ArcXml schema and generated the classes from the...
2
by: Jinsong Liu | last post by:
I have following 3 classes public class MyMainClass { MyCollection<MyObject> m_oMyObjectCollection = null; private string m_sID = string.Empty; public MyCollection<MyObject> Collection {
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.