Check if string either contains xml or json data

java, json, string, xml

Solution

An XML document entity (in common parlance, an XML document) must start with "<".

A JSON text, according to ECMA-404, starts with zero or more whitespace characters followed by one of the following:

{ [ " 0-9 - true false null

So your simplest approach is just to test `if(s.startsWith("<")`

Problem

I receive a String which contains either xml or json contents. If String contains json content I use jackson(java api) to Convert JSON to Java object And if it contains xml contents I use JAXB to Convert XML content into a Java Object(Unmarshalling). How can I check whether I receive xml or json in that string?

Original source