blob: a84e428cd5e70d247446b43f8dfd07ae9d6bf1c3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
package test;
import java.io.File;
import java.io.IOException;
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.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class TestValidateXml {
public static void main(String[] args) {
try {
// 1. Lookup a factory for the W3C XML Schema language
SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
// 2. Compile the schema.
// Here the schema is loaded from a java.io.File, but you could use
// a java.net.URL or a javax.xml.transform.Source instead.
Schema schema = factory.newSchema(new File("/opt/xml/docbook/xsd/docbook.xsd"));
// 3. Get a validator from the schema.
Validator validator = schema.newValidator();
validator.setErrorHandler(new ForgivingErrorHandler());
// 4. Parse the document you want to check.
Source source = new StreamSource(args[0]);
// 5. Check the document
try {
validator.validate(source);
System.out.println(args[0] + " is valid.");
} catch (SAXException ex) {
System.out.println(args[0] + " is not valid because ");
System.out.println(ex.getMessage());
}
} catch(IOException ex) {
System.out.println(args[0] + " is not valid because ");
System.out.println(ex.getMessage());
} catch(SAXException ex) {
System.out.println(args[0] + " is not valid because ");
System.out.println(ex.getMessage());
}
}
}
class ForgivingErrorHandler implements ErrorHandler {
public void warning(SAXParseException ex) {
System.err.println(ex.getMessage());
}
public void error(SAXParseException ex) {
System.err.println(ex.getMessage());
}
public void fatalError(SAXParseException ex) throws SAXException {
throw ex;
}
}
|