regular expression to check if string is valid XML [closed]
I am looking java code to check if a string is valid XML.
It's not possible to validate XML with regular expressions. XML is not a regular language.
Use an XML parser to try to parse the string as XML or else validate the XML document against a schema, for example a DTD or XSD file.
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);
精彩评论