abc9876543210

VALIDATE STRING XML in JAVA

String strXML ="<?xml version='1.0' encoding='UTF-8' standalone='yes'?><custDtl><name>abc</name><mobNo>9876543210</mobNo></custDtl>" 

how to check if a string is a valid XML string.

+4
source share
1 answer

You just need to open an InputStream based on an XML String and pass it to SAX Parser:

 try { String strXML ="<?xml version='1.0' encoding='UTF-8' standalone='yes'?><custDtl><name>abc</name><mobNo>9876543210</mobNo></custDtl>"; SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); InputStream stream = new ByteArrayInputStream(strXML.getBytes("UTF-8")); saxParser.parse(stream, ...); } catch (SAXException e) { // not valid XML String } 
+5
source

All Articles