On 2008-11-27, Brian Lonsdorf <brian / trnsfr.com> wrote: > Hi, i've got an xml file i'm parsing and creating objects from. > > The xml is not named, nor formatted like my objects, so i need to define > a bunch of mappings/translations. > > I'm looking for any design patterns or suggestions people might have to > elegantly solve this. > Well, if you are going to be parsing lots of different types of XML document, here are some possible ways you could approach the problem: - if a schema document is available, pull it in and do something clever with it. If it's a RelaxNG schema, if you look for oneOrMore and zeroOrMore elements that contain element references, you can use that to map to lists. - you could also write an XSLT transformation to turn the document into an intermediary XML format for which there are already intuitive interfaces in Ruby (or whatever other language you end up using) - RSS/Atom, for instance (maybe RDF eventually - I'm soon to release an alpha version of a Ruby RDF gem) or even something like XML-RPC or SOAP, both of which are designed to map to native data-types (and objects in the case of SOAP). - you could see if you could produce some kind of clever metrics from the document by looking for the most-used elements in particular places in the hierarchy. - sometimes a separate parser/serializer class is a necessity - I took this approach in my project just because it was the approach that the guys who had done the Python and Java libraries had done, and it seemed pretty sensible to do it that way. One day, people will get that XML is a *markup* language - and markup is something you add to *documents*. If you just want to shunt data around, it probably shouldn't be the first choice when compared with something like JSON or YAML. -- Tom Morris <http://tommorris.org>