I am looking java code to check if a string is valid XML.
            Asked
            
        
        
            Active
            
        
            Viewed 3,911 times
        
    2 Answers
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
