From: Chris Morris Date: 2002-03-18T12:51:14+09:00 Subject: RE: [ANN] Xml Serialization for Ruby > > > > > blah > > > > > 12345 > > Yeah, this is the way it is now, and it's a bit much. Especially with a Hash :) Actually, I was considering making the type an attribute of the instance element. So instead of this: > > > blah > > 12345 > it'd be: blah 12345 ... 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! 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." My conclusion at the time was the typing had to be stored somewhere, and C# stores it in the code, so it doesn't have to in the XML. Well, Ruby has the type, too, if the instance var is set. ...(testing, rewriting...) ... hmmm ... this should work, though I've hit upon a weirdness. It seems that the root node will always have to be the class name, at least the way the code is structured now. The to_xml call creates the instance var name node for its instance vars ... because it has this knowledge. The root to_xml call, doesn't know what the calling code is referring to it as, nor does it matter. 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? Chris