Forum Discussion
bzzup
10 years agoOccasional Contributor
I'm struggling with the same problem currently. What i've found:
However this doesn't work in my case - looks like i have some problems with my xml structure. At least point to start with
import javax.xml.XMLConstants
import javax.xml.transform.Source
import javax.xml.transform.stream.StreamSource
import javax.xml.validation.Schema
import javax.xml.validation.SchemaFactory
import javax.xml.validation.Validator
import org.xml.sax.SAXException
import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import javax.xml.transform.dom.DOMSource;
String xsd = 'http://<server>/rest/application.wadl/xsd0.xsd' //path to xsd
String xml = 'C:\\test.xml' //path to xml to validate
//Create xsd validator
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
URL schemaFile= new URL(xsd)
Schema schema = schemaFactory.newSchema(schemaFile)
Validator validator = schema.newValidator()
//Create xml source to validate
DocumentBuilderFactory bfactory = DocumentBuilderFactory.newInstance()
DocumentBuilder dbuilder = bfactory.newDocumentBuilder()
Document xsdDoc = dbuilder.parse(new File(xml))
DOMSource source = new DOMSource(xsdDoc)
validator.validate(source)
However this doesn't work in my case - looks like i have some problems with my xml structure. At least point to start with