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.
You can use an XmlAdapter to handle the date format. Since the date formats differ from doc to doc you can take advantage of specifying an initialized instance of the adapter on the Unmarshaller. In this way you could set the date format appropriate to the document you want to unmarshal.
Example
http://blog.bdoughan.com/2011/09/mixing-nesting-and-references-with.html
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.
Related
The SolrJ library offers different parsers for Solr's responses.
Namely:
BinaryResponseParser
StreamingBinaryResponseParser
NoOpResponseParser
XMLResponseParser
Sadly the documentation doesn't say much about them, other than:
SolrJ uses a binary format, rather than XML, as its default format.
Users of earlier Solr releases who wish to continue working with XML
must explicitly set the parser to the XMLResponseParser, like so:
server.setParser(new XMLResponseParser());
So it looks like the XMLResponseParser is there mainly for legacy purposes.
What are the differences between the others parsers?
Can I expect performance improvements by using an other parser over the XMLResponseParser?
The Binary Stream Parsers is meant to work directly with the Java Object Format (the binary POJO format) to make the creation of data objects as smooth as possible on the client side.
The XML parser was designed to work with the old response format where there wasn't any real alternatives (as there was no binary response format in Solr). It's a lot more work to consider all the options for an XML format than use the binary format directly.
The StreamingBinaryResponseParser does the same work as the BinaryResponseParser, but has been designed to make streaming documents (i.e. not creating a list of documents and returning that list, but instead return each document by itself without having to hold them all in memory at the same time) possible. See SOLR-2112 for a description of the feature and why it was added.
Lastly, yes, if you're using SolrJ, use the binary response format, unless you have a very good reason for using the XML based one. If you have to ask the question, you're probably better off with the binary format.
I'm trying to get date data types from an excel file, but the output when he's reading is 41306.038888888892.
This value just appear for date, is there any way to get the normal date?
I did not find anything searching in the web.
Hope someone can help
thanks
As with your previous question, I'd strongly suggest you try reading and understanding some of the various examples for this, you'll save yourself a lot of time! The two main ones probably being XSSFEventBasedExcelExtractor in Apache POI and XSSFExcelExtractorDecorator in Apache Tika
If you take the easy route, then you can just use XSSFSheetXMLHandler, which will handle all the pesky formatting stuff for you, and give you nicely formatted strings for your dates
Otherwise, if you want to stay at the low level, then you need to check the formatting rule applied to a cell. If it's a date-based format string, you then need to convert it from a number into a Date object. Handily, there's a POI class DateUtil which can both help you check if a cell is Date formatted, and convert it into a Java Date object for you
I have not found this in any documentation, tutorial or question. Pretty much all tutorials concerned date picker, so I am confused about its functionality.
I have TextView with inputType="date". How do I handle it? Does actually it do anything other than text field does?
What are best practices?
It only has set/get methods for String type. Am I right in assumption that I have to parse contents manually? And if putting in default value, having to format it myself too?
Is there any definition of expected date format? Does it handle Locale differences (mm/dd/yyyy in some, dd/mm/yyyy in other)?
Is there any validation?
Or is it basically "do it yourself" type thing without much convenience?
input type date means it will open numeric virtual keyboard on the screen when the user tap the textview. To parse it to your required format, you have to use JAVA date time class.
Input type simply specifies which kind of keyboard you want the user to use, to simplify the input. For example, a phone number input field does not require letters. Yes, you have to manually parse it, or use helper classes as stated by Mark Henry.
Textview - inputType
I am facing a problem with Xalon while converting Java object to String, i.e empty open close tags are converted to self closing tags. eg. <span></span> gets converted to </span>.
I have fixed simliar problem while using Saxon XSL transformer. Is it possible to use Saxon to convert a java Object to String instead of Xalon.
First, I'm sure you mean <span/> for the self-closing tag.
Second: why is this a problem? If you are generating XML, <span></span> means exactly the same as <span/>, and will be treated the same by any XML parser. (If you're reading the XML without an XML parser, then DON'T). On the other hand, if you are generating HTML, then specifying method="html" should be all you need to do, whether you are using Xalan or Saxon.
Third: I can't see any relationship between your serialization problem and the task of converting Java objects to strings.
You can certainly do such things in Saxon. The documentation for calling Java methods from Saxon can be found here: http://www.saxonica.com/documentation/extensibility/intro.xml (Sorry there's so much of it, but I don't know enough about your situation to give you a more precise pointer).
I'm looking for a list of common datetime formats used in logs (e.g. webserver, database, etc).
Even better would be a (java) library that can extract date and time from a given string ( < 10KB).
Does anyone know a good one?
this library is likely a good place to start: SimpleDateFormat
The docs contains the an introduction to the standard datetime format strings. But as #Olaf points out, you're going to need to specify what the format is beforehand or there is literally no way differentiate certain dates from one another.
Looks like what you'd want to do is construct a range of date formats that might match, apply all of them to a date string, then see which date is closest to Datetime.now().
Although this doesn't answer your question directly, but Java includes libraries for working with regular expressions. It would be pretty easy to write a library of your own based on that. I've has a lot of success extracting all sorts of data using regular expression. It would certainly be less than 10kb and would require no external dependencies other than the JDK.