How to validate XML against XSD without writing to a File

Validating XMLs against XSDs usually involves an XML and XSD files. However, we could validate XMLs as String objects in Java.

Marshall to String

First, we need codes to convert a JAXB object to a String object. This is done by marshaling the object to a StringWriter.

From the StringWriter object, we can generate a String object.

Validate XML String

We can use the String object to compare against an XSD.

Sample Usage


Karl San Gabriel

Karl San Gabriel

Java and Enterprise Technologies Expert