How to check if URL contains XML file or not?

Ways to know whether GETing a URL will retrieve XML...

Before retrieving the file

  • Have an out-of-band guarantee.
  • Inspect Content-Type HTTP header of response to a HEAD request1.

After retrieving the file

  • Inspect Content-Type HTTP header of the response1.
  • Sniff root element.
  • Files.probeContentType(path)
  • Parse via conforming XML parser without getting any well-formedness errors.

Note: Only parsing via a conforming XML parser is guaranteed to provide 100% determination.


1MIME assignments for XML data:

  • application/xml (RFC 7303, previously RFC 3023)
  • text/xml (RFC 7303, previously RFC 3023)
  • Other MIME assignments used with XML applications.