From: Massimiliano Mirra Date: 2002-03-19T07:54:28+09:00 Subject: Re: [ANN] Xml Serialization for Ruby On Mon, Mar 18, 2002 at 12:51:14PM +0900, Chris Morris wrote: > Actually, I was considering making the type an attribute of the instance > element. So instead of this: > > > > > > > blah > > > > 12345 > > > > it'd be: > > > > blah > > 12345 > Yes, basically the options are these two---apart from the more complex and complete UXF, SOAP and XMLRPC (as Tobias and Hiroshi suggested), whose goal however differs from yours (quick readability) and the one I had (conciseness). These two are mostly equivalent. The main reasons I prefer the first are 1) since the element name is the class name, you can always extrapolate an element and easily marshal it into an object and 2) available classes are a finite number while attribute names are virtually infinite, so it might be easier to validate a document in the first case. > That allows the simple types to be eliminated easi-- ... actually, now that > I type this, there's no need for any of the types at all is there? Can't I > query the receiving object for the type of the instance var named in the > element? ... Well, only if it's been initialized, which is not guaranteed > ... but if the object is coded to initialize each instance var, then I can > query the type at runtime and there's no need for the type in the XML at > all. Cool! Just keep in mind that preserving type information in XML might help you detect whether you are trying to marshal an older or different format to the current implementation---where, for example, might be of class Customer now, whereas it was String in the previous implementation... > This is Good. I felt I'd been overlooking something ever since I started > this when I looked at xmarshal and thought, "Odd, Ruby is dynamic, yet this > XML is typed. C# is typed, and its serialized XML is not." (Well, dynamic doesn't mean untyped, it just means dinamically typed, doesn't it?) > So ... assuming MyClass initializes its instance vars to the type they > should be when the XML is read in, the XML can be: > > > > blah > > 12345 > > > Which is cool. But, I'll have to a little jogging around of code so that the > initial to_xml call knows to create a root type node, and all subsequent > calls know not to. > > Then, adding an option to store the type as an attribute for anything other > than the root node is a simple switch ... that way anyone without a fully > initialized object (or one who's type should change when the XML is read > in -- weird case, but weird happens) is covered as well. > > Thoughts? If you *really* want to follow that scheme, I'd suggest just adding the special name ``root'': ... ...and possibly make all the type attributes inside root optional. At least it would stop mixing class names and attribute names as the element names. And if also you want to consider the other way, watch your mailbox. :-) Massimiliano