Purpose: This file determines if the given type (XML, JSON, properties, etc.)
Consider the XML-Up case, until we ran into this problem, the following sample approach worked fine:
try { saxReader.read(f); } catch (DocumentException e) { logger.warn(" - File is not XML: " + e.getMessage()); return false; } return true;
As expected, when the XML is well-formed, the test passes and the method returns true. If something bad happens and the file cannot be parsed, false is returned.
This violates, however, when we are dealing with malformed XML (still an XML file).
I would rather not rely on the .xml
extension (doesn't work all the time), look for the string <?xml version="1.0" encoding="UTF-8"?>
Inside the file, etc.
Is there any other way to handle this?
What would you see inside the file to "suspect that it might be XML
, although DocumentException
been caught." This is necessary for parsing.
source share