Determine if a String is XML using Java and Regular Expressions
January 29, 2009 1 Comment
So again I am posting something I have to do every now and then and have to spend time, each time, to check the pattern or usage etc. for.
Once in a while, in an app that does not do much XML, and therefore is not already using an XML parser of some kind, will need to at the least, determine if a String is XML. With a pretty simple Regular Expression, it is possible using plain old Java and without using any specific XML technology.
I know there are other references out there for doing this, but it is here below as a code sample, for my easy reference and maybe it will help someone else out, who knows. Enjoy.
Are we XML (like) data? :
import java.util.regex.Pattern; import java.util.regex.Matcher; public class test { /** * return true if the String passed in is something like XML * * * @param inString a string that might be XML * @return true of the string is XML, false otherwise */ public static boolean isXMLLike(String inXMLStr) { boolean retBool = false; Pattern pattern; Matcher matcher; // REGULAR EXPRESSION TO SEE IF IT AT LEAST STARTS AND ENDS // WITH THE SAME ELEMENT final String XML_PATTERN_STR = "<(\\S+?)(.*?)>(.*?)</\\1>"; // IF WE HAVE A STRING if (inXMLStr != null && inXMLStr.trim().length() > 0) { // IF WE EVEN RESEMBLE XML if (inXMLStr.trim().startsWith("<")) { pattern = Pattern.compile(XML_PATTERN_STR, Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.MULTILINE); // RETURN TRUE IF IT HAS PASSED BOTH TESTS matcher = pattern.matcher(inXMLStr); retBool = matcher.matches(); } // ELSE WE ARE FALSE } return retBool; } }/**/