-
Notifications
You must be signed in to change notification settings - Fork 1
Usage examples
Ross Lamont edited this page Nov 24, 2017
·
8 revisions
All validators are typically loaded using the mechanisms mentioned in the JAXP documentation.
Say we want to validate the following xml file:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-model href="http://componentcorp.com/schema/jxvc/test/simpleRootSchema.xsd"
schematypens="http://www.w3.org/2001/XMLSchema" ?>
<root xmlns="http://componentcorp.com/schema/jxvc/test/simpleRootSchema">
<child/>
</root>
Note the xml-model
processing instruction which identifies our schema, according to
ISO/IEC19757-11.
To validate this file, you first need the following maven dependency in your java project:
<dependency>
<groupId>com.componentcorp.xml.validation</groupId>
<artifactId>jxvc</artifactId>
<version>0.9.1</version>
</dependency>
Next, write your code:
package com.componentcorp.xml.validation.example;
import static com.componentcorp.xml.validation.test.helpers.BaseXMLValidationTest.INTRINSIC_NS_URI;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.w3c.dom.ls.LSResourceResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
public class Sample1 {
public Result validate(File f,ErrorHandler errorHandler, LSResourceResolver resourceResolver)
throws SAXException, FileNotFoundException, IOException
{
SchemaFactory schemaFactory = SchemaFactory.newInstance(INTRINSIC_NS_URI);
Schema schema = schemaFactory.newSchema();
final Validator validator = schema.newValidator();
validator.setErrorHandler(errorHandler);
validator.setResourceResolver(resourceResolver);
InputStream is = new FileInputStream(f);
Source source = new StreamSource(is);
Result result = new StreamResult();
validator.validate(source, result);
return result;
}
}
Validating against another sort of schema is almost as simple. The only substantial difference is you have to make sure the right validation libraries are available. First, the xml file.
<?xml version="1.0" encoding="UTF-8"?>
<?xml-model href="http://componentcorp/schema/jxvc/test/simpletest.rnc"
schematypens="http://www.iana.org/assignments/media-types/application/relax-ng-compact-syntax" ?>
<root xmlns="http://componentcorp.com/schema/jxvc/test/simpleRootSchema">
<child/>
</root>
Next the maven dependencies, including our additional validation library for Relax NG Compact
<dependency>
<groupId>com.componentcorp.xml.validation</groupId>
<artifactId>jxvc</artifactId>
<version>0.9.1</version>
</dependency>
<dependency>
<groupId>com.componentcorp.xml.validation</groupId>
<artifactId>relaxng-compact</artifactId>
<version>0.9.1</version>
</dependency>
The java code is identical to the first example.