xsdValidataTest.java
1.4 KB
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
import javax.xml.XMLConstants;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import java.io.File;
import org.xml.sax.SAXException;
import java.io.IOException;
public class xsdValidataTest {
public static void validateXmlAgainstXsd(File xmlFile, File cebXsdFile, File signatureXsdFile) throws SAXException, IOException {
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
// 加载多个 XSD 文件
Schema schema = schemaFactory.newSchema(new StreamSource[] {
new StreamSource(cebXsdFile),
new StreamSource(signatureXsdFile) // 加载签名的 XSD 文件
});
Validator validator = schema.newValidator();
validator.validate(new StreamSource(xmlFile));
System.out.println("XML is valid.");
}
public static void main(String[] args) {
File xmlfile = new File("transmit/CEB603Message.xml");
File cebXsdFile = new File("xsd/CEB603Message.xsd");
File signatureXsdFile = new File("xsd/xmldsig-core-schema.xsd");
try {
validateXmlAgainstXsd(xmlfile, cebXsdFile, signatureXsdFile);
} catch (SAXException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}