java xml出错,在Java中针对XSD 1.1验证XML时出错

我有一个例外说:

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

然后我试了 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();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值