I have this regex based XML
validator that I would like to use for recognizing
XML string. Say, I have the following XML
String,
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<molecules>
<molecule id="1">
<atoms>
<atom id="1" symbol="C"/>
<atom id="2" symbol="C"/>
<atom id="3" symbol="N"/>
</atoms>
<bonds>
<bond atomAId="1" atomBId="2" id="1" order="SINGLE"/>
<bond atomAId="2" atomBId="3" id="2" order="DOUBLE"/>
</bonds>
</molecule>
</molecules>
I use the following validator for the XML
,
public static boolean isValidXML(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;
}
However, the methods returns false
even for the valid XML
as well. How do I correct the isValidXML
method?