I have some content and I would like to know whether they are XML
or not. How to do that ? I would only need to know the answer true
or false
from a method return type. I plan to use REgex but open for better suggestions.
The XML
content is as following and will be always in the same format (may be the molecule ID will be increased or decreased),
<?xml version="1.0" encoding="UTF-8"?>
<molecules>
<molecule id="1">
<atoms>
<atom id="1" symbol="C"/>
<atom id="2" symbol="C"/>
<atom id="3" symbol="N"/>
</atoms>
<bonds>
<bond id="1" atomAId="1" atomBId="2" order="SINGLE"/>
<bond id="2" atomAId="2" atomBId="3" order="DOUBLE"/>
</bonds>
</molecule>
<molecule id="2">
<atoms>
<atom id="1" symbol="C"/>
<atom id="2" symbol="C"/>
<atom id="3" symbol="N"/>
</atoms>
<bonds>
<bond id="1" atomAId="1" atomBId="2" order="SINGLE"/>
<bond id="2" atomAId="2" atomBId="3" order="DOUBLE"/>
</bonds>
</molecule>
</molecules>
I make the Regex
to recognize the XML
as following,
public static final String REGEX_FOR_XML = "((<(\\S(.*?))(\\s.*?)?>(.*?)<\\/\\3>)|(<\\S(.*?)(.*?)(\\/>)))";
The issue is it only matches with the inner content while I would like to make an entire content match. I use this validator for matching,
public static boolean isValidXML(String inXMLStr) {
if (inXMLStr == null || inXMLStr.isEmpty())
return false;
final Pattern pattern = Pattern.compile(Constants.REGEX_FOR_XML);
if (pattern.matcher(inXMLStr).matches()) {
return true;
}
return false;
}
How can I correct the Regex
to match with the XML
content or what to do as better option ?