0

I have the following reoccurring pattern: I call a black box with some parameters and get as a result which is List<Map<String, Object>> (or something equivalent) where each Map has the same keySet. Then, I'm expected to produce XML of the form

<list>
    <item key1='value1' ...>
        <keyX>valueX</keyX>
    </item>
</list>

The list and item elements' names and whether certain key will be sub-element or attribute of item vary with individual task. I'd like to minimize hand-wiring of the Map keys to XML.

The way I imagined it at first is to create a XML template that would describe map keys to element/attribute mapping. Then I figured that's what XSD is for, kind of, so I could use that format to define a template. However I couldn't find good solution to generate XML from XSD. I had a look at JAXB and it seems too heavyweight for my scenarios: I do not need Java classes - having domain objects would be nice, except I never do anything with the data. Then, I'd have to map my data to the classes first, which seems like inefficient thing to do for just dumping them with the marshaller. And second, I'd like to keep more flexibility - if the data format (i.e. keySet in each Map) changes I'd only have to modify the schema and not regenerate sources and rebuild.

Thanks for reading through :)

sbk
  • 9,212
  • 4
  • 32
  • 40
  • Just thought a bit more about it, and I think, it should also be possible to do this with JAXB, but you will need to describe the expected format a bit more thoroughly, for us to determine whether it is sensible to do this through customising the JAXB marshalling. – tveon Mar 08 '12 at 12:39

1 Answers1

0

I remember doing something like this a year or so ago. What I did, was to write and XSLT, which took some parameters and processed it with JAXP to produce the XML.

If you are new to XSL, it might be a bit tricky, but there are lots of good documentation around. :)

tveon
  • 579
  • 5
  • 24