有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在Java中根据XSD 1.1验证XML时出错

我试图用Java中的XSD1.1验证XML。如问题How to validate XML against XSD 1.1 in Java?

我得到一个例外,说:

java.lang.IllegalArgumentException: No SchemaFactory that implements the schema language specified by: http://www.w3.org/XML/XMLSchema/v1.1 could be loaded

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

Cannot make a static reference to the non-static method setProperty(String, Object) from the type SchemaFactory

我包括的这些罐子仍然没有错误的变化

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中的XSD1.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) 个答案

  1. # 1 楼答案

    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");