我正在尝试使用oasis目录验证xml文件。我需要的是提供xml文件的路径和包含xsd的目录的路径作为输入,并将验证(true或error message)作为输出。
到目前为止,我所做的是:
File schemaFile = new File("personal.xsd"); // etc.
Source xmlFile = new StreamSource(new File("personal.xml"));
SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/XML/XMLSchema/v1.1"); // we need to validate with v1.1
try {
Schema schema = schemaFactory.newSchema(schemaFile);
Validator validator = schema.newValidator();
validator.validate(xmlFile);
System.out.println(xmlFile.getSystemId() + " is valid");
} catch (SAXException e) {
System.out.println(xmlFile.getSystemId() + " is NOT valid reason:" + e);
} catch (IOException e) {
e.printStackTrace();
}因此,到目前为止,它是有效的。
问题是添加目录。我找到了一些代码示例,但它不起作用:
Source xmlFile = new StreamSource(new File("personal.xml"));
String [] catalogs = {"file:///catalog.xml"};
SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/XML/XMLSchema/v1.1");
try {
XMLCatalogResolver resolver = new XMLCatalogResolver();
resolver.setPreferPublic(true);
resolver.setCatalogList(catalogs);
schemaFactory.setResourceResolver(resolver);
Schema schema = schemaFactory.newSchema();
Validator validator = schema.newValidator();
validator.validate(xmlFile);}
最后,找不到xsd,验证失败。
有人能给我解释一下我哪里做错了吗?我觉得这应该很简单,但我遗漏了一些东西。
谢谢。
发布于 2021-06-18 22:04:33
经过两周的研究,我的结论是
我从来没有用Xerces-2j做到这一点。您可以使用JRE (javaSE-1.8)中的xerces来完成此任务,下面是如何完成此任务的方法。
import java.io.File;
import java.io.StringWriter;
import java.net.URL;
//import javax.xml.validation.SchemaFactory;
import java.net.URI;
import java.net.URISyntaxException;
import javax.xml.XMLConstants;
//import javax.xml.catalog.CatalogFeatures;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Schema;
import javax.xml.validation.Validator;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class Foo
{
private static int errorCount = 0;
public static void main(String[] args)
{
final String catalogFile = "javax.xml.catalog.files";//CatalogFeatures.Feature.FILES.getPropertyName();
final String catalogPath = "catalog.xml";
final ClassLoader classLoader = Foo.class.getClassLoader();
try
{
final URL catalogUrl = classLoader.getResource(catalogPath);
final URI catalog = catalogUrl.toURI();
if (catalog != null)
{
System.out.println("catalog : "+ catalogFile);
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
// SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/XML/XMLSchema/v1.1");
Schema schema = schemaFactory.newSchema();
StreamSource source = new StreamSource(new File("personal.xml"));
Validator validator = schema.newValidator();
validator.setProperty(catalogFile, catalog.toString());
validator.setErrorHandler(new MyErrorHandler());
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
validator.validate(source, result);
System.out.println(writer);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
private static class MyErrorHandler implements ErrorHandler {
public void warning(SAXParseException e) throws SAXException {
System.out.println("Warning: ");
printException(e);
}
public void error(SAXParseException e) throws SAXException {
System.out.println("Error: ");
printException(e);
}
public void fatalError(SAXParseException e) throws SAXException {
System.out.println("Fattal error: ");
printException(e);
}
private void printException(SAXParseException e) {
errorCount++;
System.out.println(" Line number: "+e.getLineNumber());
System.out.println(" Column number: "+e.getColumnNumber());
System.out.println(" Message: "+e.getMessage());
System.out.println();
}
}
}如果您想使用xerces-2J,请使用GL
https://stackoverflow.com/questions/67637445
复制相似问题