you can check whether document or text is well-formed, by using a parser.
i think this link will help you:
http://www.roseindia.net/xml/dom/DOMParserCheck.shtml
for string replace this line in the link:
InputSource is = new InputSource(xmlFile);
with
ByteArrayInputStream stringStream=new ByteArrayInputStream("XML Text".getBytes());
InputSource is=new InputSource(stringStream);