-6

I am looking java code to check if a string is valid XML.

Manish
  • 25
  • 1
  • 6
  • 7
    Don't try it! Read: http://stackoverflow.com/questions/701166/can-you-provide-some-examples-of-why-it-is-hard-to-parse-xml-and-html-with-a-rege – Michał Niklas Sep 27 '11 at 06:24

2 Answers2

7

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.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
-3

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);

Javaeg
  • 11
  • 3