在Java

时间:2016-10-13 12:32:53

标签: java xml validation xsd xsd-1.1

我正在尝试使用Java在XSD 1.1中验证XML。如问题How to validate XML against XSD 1.1 in Java?

我有一个例外说:

  

java.lang.IllegalArgumentException:无法加载实现由http://www.w3.org/XML/XMLSchema/v1.1指定的模式语言的SchemaFactory

然后我尝试了SchemaFactory.setProperty("http://saxon.sf.net/feature/xsd-version", "1.1"),错误是:

  

无法从SchemaFactory类型对非静态方法setProperty(String,Object)进行静态引用

我包含的这些jar仍然没有改变错误。

java-cup-10k.jar

org.eclipse.wst.xml.xpath2.processor-2.1.100.jar

xercesImpl-2.11.0.jar

xml-apis-xerces-2.7.1.jar

任何人都可以帮我在Java中针对XSD 1.1验证XML吗?

import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Schema;
import javax.xml.XMLConstants;
import javax.xml.transform.sax.SAXSource;
import org.xml.sax.InputSource;
import javax.xml.validation.Validator;
import java.io.*;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.ErrorHandler;
class Xsd11SchemaValidator {
  private static int errorCount = 0;
  public static void main(String[] a) {
    if (a.length<2) {
      System.out.println("Usage:");
      System.out.println("java Xsd11SchemaValidator schema_file_name "
        + "xml_file_name");
    } 
      String schemaName ="C:\\Ankit\\tempFiles\\test1.xsd";
      String xmlName ="C:\\Ankit\\tempFiles\\test1.xml";
      Schema schema = loadSchema(schemaName);
      validateXml(schema, xmlName);

  }
  public static void validateXml(Schema schema, String xmlName) {
    try {
      // creating a Validator instance
      Validator validator = schema.newValidator();

      // setting my own error handler
      validator.setErrorHandler(new MyErrorHandler());

      // preparing the XML file as a SAX source
      SAXSource source = new SAXSource(
        new InputSource(new java.io.FileInputStream(xmlName)));

      // validating the SAX source against the schema
      validator.validate(source);
      System.out.println();
      if (errorCount>0) {
        System.out.println("Failed with errors: "+errorCount);
      } else {
        System.out.println("Passed.");
      } 

    } catch (Exception e) {
      // catching all validation exceptions
      System.out.println();
      System.out.println(e.toString());
    }
  }
  public static Schema loadSchema(String name) {
    Schema schema = null;
    try {

这里我需要帮助

//      String language = XMLConstants.W3C_XML_SCHEMA_NS_URI;
      String language = "http://www.w3.org/XML/XMLSchema/v1.1";
      SchemaFactory.setProperty("http://saxon.sf.net/feature/xsd-version", "1.1");
      SchemaFactory factory = SchemaFactory.newInstance(language);


      schema = factory.newSchema(new File(name));
    } catch (Exception e) {
      System.out.println(e.toString());
    }
    return schema;
  }
  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();
    }
  }
}

1 个答案:

答案 0 :(得分:0)

SchemaFactory.setProperty("http://saxon.sf.net/feature/xsd-version","1.1");
SchemaFactory factory = SchemaFactory.newInstance(language);

需要更改为:

SchemaFactory factory = SchemaFactory.newInstance(language);
factory.setProperty("http://saxon.sf.net/feature/xsd-version", "1.1");