我在Java 1.6上用下面一行解决了这个错误:
// Specify you want a factory for RELAX NG "compact"
System.setProperty(SchemaFactory.class.getName() + ":" + XMLConstants.RELAXNG_NS_URI, "com.thaiopensource.relaxng.jaxp.CompactSyntaxSchemaFactory");
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.RELAXNG_NS_URI);
这允许我使用 Jing 根据 Compact RELAX NG 模式验证 XML 文档。完整示例如下。我没有使用桥或其他任何东西。运行时类路径只有 jing.jar (20091111) 和我自己的 Validator 类。
import java.io.File;
import java.io.IOException;
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 org.xml.sax.SAXException;
public class Validate
{
public static void main(String[] args) throws SAXException, IOException
{
// Specify you want a factory for RELAX NG
System.setProperty(SchemaFactory.class.getName() + ":" + XMLConstants.RELAXNG_NS_URI, "com.thaiopensource.relaxng.jaxp.CompactSyntaxSchemaFactory");
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.RELAXNG_NS_URI);
// Load the specific schema you want.
// Here I load it from a java.io.File, but we could also use a
// java.net.URL or a javax.xml.transform.Source
File schemaLocation = new File(args[0]);
// Compile the schema.
Schema schema = factory.newSchema(schemaLocation);
// Get a validator from the schema.
Validator validator = schema.newValidator();
for (int i = 1; i < args.length; i++)
{
String file = args[i];
// Check the document
try
{
validator.validate(new StreamSource(new File(file)));
System.out.println(file + " is valid.");
}
catch (SAXException ex)
{
System.out.print(file + " is not valid because: " + ex.getMessage());
}
}
}
}
再一次,我只测试了这个离子Java 1.6。
$ java -version
java version "1.6.0_01"
Java(TM) SE Runtime Environment (build 1.6.0_01-b06)
Java HotSpot(TM) Client VM (build 1.6.0_01-b06, mixed mode, sharing)