![](/img/trans.png)
[英]Xerces2-j XML Schema Attribute / Element Declaration data type
[英]How to Validate a xml file with catalogs using Xerces2-j
我正在嘗試使用 oasis 目錄驗證 xml 文件。 我需要的是提供 xml 文件的路徑和包含 xsd 作為輸入的目錄的路徑,並獲得驗證(真或錯誤消息)為 Z78E6221F6393D1356681DB398D
到目前為止我所做的是:
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 沒有找到,驗證失敗。
有人可以解釋我做錯了什么嗎? 我覺得它應該很簡單,但我錯過了一些東西。
感謝。
經過兩周的研究,我的結論
我從來沒有設法用 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();
}
}
}
GL 如果你想使用 xerces-2J
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.