SchemaFactory schemaFactory = SchemaFactory .newInstance(XMLConstants.XML_DTD_NS_URI); Schema schema = schemaFactory.newSchema(new File( "xmlValidate.dtd")); Validator validator = schema.newValidator(); validator.validate(new StreamSource("xmlValidate.xml"));
... Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI) .newSchema(new File("Item.xsd")); unmarshaller.setSchema(schema); //register item.xsd shcema for validation ...
public FlowParser() throws SAXException { schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); flowSchema = schemaFactory.newSchema(FileAuthorizer.class.getResource(FLOW_XSD)); }
SchemaFactory factory = SchemaFactory .newInstance("http://www.w3.org/2001/XMLSchema"); File file = new File("test.xml"); Schema schema = factory.newSchema(file); JAXBContext context = JAXBContext.newInstance(ACCESREFUSE.class); Unmarshaller u = context.createUnmarshaller(); u.setSchema(schema); Object anObject = u.unmarshal(new StreamSource(new StringReader( getMessage())), ACCESREFUSE.class);
public StoreDefinitionsMapper() { try { SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Source source = new StreamSource(StoreDefinitionsMapper.class.getResourceAsStream("stores.xsd")); this.schema = factory.newSchema(source); } catch(SAXException e) { throw new MappingException(e); } }
try { schemaFactory = SchemaFactory .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); schema = schemaFactory.newSchema(new Source[] { new StreamSource(new FileInputStream(xsd1)), new StreamSource(new FileInputStream(xsd2)) }); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (SAXException e1) { // TODO Auto-generated catch block e1.printStackTrace(); }
private Validator getValidator(URL xmlSchema) throws SAXException { SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = sf.newSchema(xmlSchema); Validator validator = schema.newValidator(); validator.setErrorHandler(new ErrorHandler()); return validator; }
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); URL schemaURL = // The URL to your XML Schema; Schema schema = sf.newSchema(schemaURL); Validator validator = schema.newValidator(); DOMSource source = new DOMSource(xmlDOM); validator.validate(source);
static boolean validateAgainstXSD(InputStream xml, InputStream xsd) { try { SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = factory.newSchema(new StreamSource(xsd)); Validator validator = schema.newValidator(); validator.validate(new StreamSource(xml)); return true; } catch(Exception ex) { return false; } }
SchemaFactory sf = SchemaFactory.newInstance( javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = sf.newSchema(new File("myschema.xsd")); unmarshaller.setSchema(schema);
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Source schemaFile = new StreamSource(new File(namespace + ".xsd")); Schema schema = factory.newSchema(schemaFile); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware( true ); dbf.setValidating(true); dbf.setSchema(schema);
public ClusterMapper() { try { SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Source source = new StreamSource(ClusterMapper.class.getResourceAsStream("cluster.xsd")); this.schema = factory.newSchema(source); } catch(SAXException e) { throw new MappingException(e); } }
protected Schema createSchema() throws SAXException { SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = null; if (classloader != null) { schema = factory.newSchema(classloader.getResource(BPMN_XSD)); } if (schema == null) { schema = factory.newSchema(BpmnXMLConverter.class.getClassLoader().getResource(BPMN_XSD)); } if (schema == null) { throw new XMLException("BPMN XSD could not be found"); } return schema; }
JAXBContext jc = JAXBContext.newInstance(Something.class); final List<ByteArrayOutputStream> outs = new ArrayList<ByteArrayOutputStream>(); jc.generateSchema(new SchemaOutputResolver(){ @Override public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); outs.add(out); StreamResult streamResult = new StreamResult(out); streamResult.setSystemId(""); return streamResult; }}); StreamSource[] sources = new StreamSource[outs.size()]; for (int i=0; i<outs.size(); i++) { ByteArrayOutputStream out = outs.get(i); // to examine schema: System.out.append(new String(out.toByteArray())); sources[i] = new StreamSource(new ByteArrayInputStream(out.toByteArray()),""); } SchemaFactory sf = SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI ); m.setSchema(sf.newSchema(sources)); m.marshal(docs, new DefaultHandler()); // performs the schema validation
import javax.xml.XMLConstants; import javax.xml.transform.Source; import javax.xml.transform.stream.StreamSource; import javax.xml.validation.*; ... URL schemaFile = new URL("http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"); Source xmlFile = new StreamSource(new File("web.xml")); SchemaFactory schemaFactory = SchemaFactory .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = schemaFactory.newSchema(schemaFile); Validator validator = schema.newValidator(); try { validator.validate(xmlFile); System.out.println(xmlFile.getSystemId() + " is valid"); } catch (SAXException e) { System.out.println(xmlFile.getSystemId() + " is NOT valid"); System.out.println("Reason: " + e.getLocalizedMessage()); }
/** * Validate the document against a given schema. Error will be written to {@link java.lang.System#err} * * @param xsdFilename schema to validate against. * @param in document to be validated. * @param options to be applied during parsing. * @throws Exception if an error occurs when parsing the document or schema. */ public static void validate(final String xsdFilename, final InputStream in, final ParserOptions options) throws Exception { final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); factory.setSchema(schemaFactory.newSchema(new File(xsdFilename))); factory.setNamespaceAware(true); if (options.xIncludeAware()) { factory.setXIncludeAware(true); factory.setFeature("http://apache.org/xml/features/xinclude/fixup-base-uris", false); } factory.newDocumentBuilder().parse(in); }
private LoginIdentityProviders loadLoginIdentityProvidersConfiguration() throws Exception { final File loginIdentityProvidersConfigurationFile = properties.getLoginIdentityProviderConfigurationFile(); // load the users from the specified file if (loginIdentityProvidersConfigurationFile.exists()) { try { // find the schema final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); final Schema schema = schemaFactory.newSchema(LoginIdentityProviders.class.getResource(LOGIN_IDENTITY_PROVIDERS_XSD)); // attempt to unmarshal XMLStreamReader xsr = XmlUtils.createSafeReader(new StreamSource(loginIdentityProvidersConfigurationFile)); final Unmarshaller unmarshaller = JAXB_CONTEXT.createUnmarshaller(); unmarshaller.setSchema(schema); final JAXBElement<LoginIdentityProviders> element = unmarshaller.unmarshal(xsr, LoginIdentityProviders.class); return element.getValue(); } catch (SAXException | JAXBException e) { throw new Exception("Unable to load the login identity provider configuration file at: " + loginIdentityProvidersConfigurationFile.getAbsolutePath()); } } else { throw new Exception("Unable to find the login identity provider configuration file at " + loginIdentityProvidersConfigurationFile.getAbsolutePath()); } }
@SuppressWarnings("deprecation") // on JDK 9 private Schema loadSchema(Resource[] resources, String schemaLanguage) throws IOException, SAXException { if (logger.isDebugEnabled()) { logger.debug("Setting validation schema to " + StringUtils.arrayToCommaDelimitedString(this.schemaResources)); } Assert.notEmpty(resources, "No resources given"); Assert.hasLength(schemaLanguage, "No schema language provided"); Source[] schemaSources = new Source[resources.length]; XMLReader xmlReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader(); xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true); for (int i = 0; i < resources.length; i++) { Resource resource = resources[i]; Assert.isTrue(resource != null && resource.exists(), () -> "Resource does not exist: " + resource); InputSource inputSource = SaxResourceUtils.createInputSource(resource); schemaSources[i] = new SAXSource(xmlReader, inputSource); } SchemaFactory schemaFactory = SchemaFactory.newInstance(schemaLanguage); if (this.schemaResourceResolver != null) { schemaFactory.setResourceResolver(this.schemaResourceResolver); } return schemaFactory.newSchema(schemaSources); }
public static void validate(final Node node, final String schemaFile) throws Exception { SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = factory.newSchema(new URL(findResource(schemaFile).toURI().toASCIIString())); Validator validator = schema.newValidator(); // validate the DOM tree try { validator.validate(new DOMSource(node)); } catch (SAXException e) { ActiveMQClientLogger.LOGGER.errorOnXMLTransformInvalidConf(e); throw new IllegalStateException("Invalid configuration", e); } }
@OnScheduled public void parseSchema(final ProcessContext context) throws IOException, SAXException { try { final File file = new File(context.getProperty(SCHEMA_FILE).evaluateAttributeExpressions().getValue()); // Ensure the file exists if (!file.exists()) { throw new FileNotFoundException("Schema file not found at specified location: " + file.getAbsolutePath()); } final SchemaFactory schemaFactory = SchemaFactory.newInstance(SCHEMA_LANGUAGE); final Schema schema = schemaFactory.newSchema(file); this.schemaRef.set(schema); } catch (final SAXException e) { throw e; } }