javaxmldatejaxbxmladapter

XMLAdapter: Configurable Date Format


I'm unmarshaling XML data using JAXB (and a little Saxon for XSLTs). In my XML document I have a date string value.

I did some research and experimented with XMLAdapter and @XMLJavaTypeAdapter. I found a useful Q&A on StackOverflow here: jaxb unmarshal timestamp. The solution outlined there involving a SimpleDateFormat seems to work well.

Here is my problem: The format of the date string in the XML is variable from document to document. It can be in a different date format each time. In the current system (that I am replacing with all this XML manipulation) there are date formats in database tables that are retrieved and applied when the XML document is parsed. Manually. Line by line. (Now you see why I'm replacing it with JAXB, yeesh!)

So the question would be: How would I change out the date format string being fed to the SimpleDateFormat in the XMLAdapter for different documents? Is such a thing even possible? Am I doomed to reading the date string out as a String and then converting it to a Date somewhere else later on?

Edit: I was just going to delete this question, but it got up-voted, so I guess I'll write out a proper answer to it as to what I ended up doing. I'm going to hold off on accepting my own answer for awhile, in case anyone has some insight that leads to an even better answer.


Solution

  • Here is what I ended up going with: I applied the exact solution found in the Q&A I linked to in my question, found here: jaxb unmarshal timestamp.

    You may be thinking: "This would appear to leave this guy with the same problem he had originally. He still has a static date format string in his SimpleDateFormat object, but his date strings are still of a variable format! He hasn't solved anything! What an idiot!"

    Here is my solution: I'm already applying an XSLT to my XML document; I have a different XSLT for each XML document type. So I'm going to take the XSLT I already (have to) have, and add functionality in it to convert the date string to the same date format as the one specified in my SimpleDateFormat object in my XMLAdapter.

    Ta da! I don't need to configure jack now. My configuration is inherent in the XSLT I already (and always) apply before I attempt to unmarshal.