Android SchemaFactory.newInstance()异常
我试图对照Android模式检查xml,但在函数的第一行,当创建模式工厂实例时,我遇到了一个异常 例外行: schemaFactory=schemaFactory.newInstance(xmlstants.W3C\u XML\u SCHEMA\u NS\u URI) 我也使用了和,但在开始时得到了相同的异常 我看到很多其他人也有同样的问题,比如,但是我还没有找到这个问题的答案 仅供参考-我正在以下功能中使用它:Android SchemaFactory.newInstance()异常,android,xml-parsing,xsd,sax,xml-validation,Android,Xml Parsing,Xsd,Sax,Xml Validation,我试图对照Android模式检查xml,但在函数的第一行,当创建模式工厂实例时,我遇到了一个异常 例外行: schemaFactory=schemaFactory.newInstance(xmlstants.W3C\u XML\u SCHEMA\u NS\u URI) 我也使用了和,但在开始时得到了相同的异常 我看到很多其他人也有同样的问题,比如,但是我还没有找到这个问题的答案 仅供参考-我正在以下功能中使用它: public static boolean validateWithExtXSDU
public static boolean validateWithExtXSDUsingSAX(String xml, String xsd) throws
ParserConfigurationException, IOException {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(false);
factory.setNamespaceAware(true);
SchemaFactory schemaFactory = null;
try {
schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
} catch (Exception e) {
System.out.println("schema factory error" + e.getMessage());
}
SAXParser parser = null;
try {
factory.setSchema(schemaFactory.newSchema(new Source[] { new StreamSource(xsd) }));
parser = factory.newSAXParser();
} catch (SAXException se) {
System.out.println("SCHEMA : " + se.getMessage()); // problem in
// the XSD
// itself
return false;
}
XMLReader reader = parser.getXMLReader();
reader.setErrorHandler(
new ErrorHandler() {
public void warning(SAXParseException e) throws SAXException {
System.out.println("WARNING: " + e.getMessage()); // do
// nothing
}
public void error(SAXParseException e) throws SAXException {
System.out.println("ERROR : " + e.getMessage());
throw e;
}
public void fatalError(SAXParseException e) throws SAXException {
System.out.println("FATAL : " + e.getMessage());
throw e;
}
});
reader.parse(new InputSource(xml));
return true;
} catch (ParserConfigurationException pce) {
throw pce;
} catch (IOException io) {
throw io;
} catch (SAXException se) {
return false;
}
}
编辑:
Android原始版本中包含的Java XML验证程序存在一些问题。您可以尝试改用Xerces,您可以从此处下载:
虽然下载部分没有下载内容,但您可以通过SVN签出来下载源代码。我也遇到过同样的问题,发现了很多类似的问题,但没有关于如何下载的好例子。以下是我使用Xerces for Android所做的工作,以使我的东西正常工作。祝你好运:) 以下几点对我很有用:
download xerces-for-android
download silk svn (for windows users) from http://www.sliksvn.com/en/download
install silk svn (I did complete install)
Once the install is complete, you should have svn in your system path.
Test by typing "svn" from the command line.
I went to my desktop then downloaded the xerces project by:
svn checkout http://xerces-for-android.googlecode.com/svn/trunk/ xerces-for-android-read-only
You should then have a new folder on your desktop called xerces-for-android-read-only
使用上面的jar(最终我会将其制作成一个jar,直接复制到我的源代码中进行快速测试。如果您希望这样做,可以使用Ant()快速制作jar),我能够获得以下用于xml验证的内容:
import java.io.File;
import java.io.IOException;
import mf.javax.xml.transform.Source;
import mf.javax.xml.transform.stream.StreamSource;
import mf.javax.xml.validation.Schema;
import mf.javax.xml.validation.SchemaFactory;
import mf.javax.xml.validation.Validator;
import mf.org.apache.xerces.jaxp.validation.XMLSchemaFactory;
import org.xml.sax.SAXException;
/**
* A Utility to help with xml communication validation.
*/public class XmlUtil {
/**
* Validation method.
*
* @param xmlFilePath The xml file we are trying to validate.
* @param xmlSchemaFilePath The schema file we are using for the validation. This method assumes the schema file is valid.
* @return True if valid, false if not valid or bad parse or exception/error during parse.
*/
public static boolean validate(String xmlFilePath, String xmlSchemaFilePath) {
// Try the validation, we assume that if there are any issues with the validation
// process that the input is invalid.
try {
SchemaFactory factory = new XMLSchemaFactory();
Source schemaFile = new StreamSource(new File(xmlSchemaFilePath));
Source xmlSource = new StreamSource(new File(xmlFilePath));
Schema schema = factory.newSchema(schemaFile);
Validator validator = schema.newValidator();
validator.validate(xmlSource);
} catch (SAXException e) {
return false;
} catch (IOException e) {
return false;
} catch (Exception e) {
// Catches everything beyond: SAXException, and IOException.
e.printStackTrace();
return false;
} catch (Error e) {
// Needed this for debugging when I was having issues with my 1st set of code.
e.printStackTrace();
return false;
}
return true;
}
}
一些旁注:
为了创建文件,我制作了一个简单的文件实用程序来向文件写入字符串:
public static void createFileFromString(String fileText, String fileName) {
try {
File file = new File(fileName);
BufferedWriter output = new BufferedWriter(new FileWriter(file));
output.write(fileText);
output.close();
} catch ( IOException e ) {
e.printStackTrace();
}
}
我还需要写一个我可以访问的区域,所以我利用了:
String path = this.getActivity().getPackageManager().getPackageInfo(getPackageName(), 0).applicationInfo.dataDir;
有点老套,它很管用。我相信有一种更简洁的方法可以做到这一点,不过我想我会分享我的成功,因为我没有找到任何好的例子。从google repository下载jar文件的链接
如果上面的链接不可用,请使用此页面下载:我也有同样的问题…@tommyk检查我的新编辑,如果有帮助,请告诉我!您好,我正在为一个方法编写Junit,该方法在内部调用一个responsevalidator方法,该方法有两行代码SchemaFactory sf=SchemaFactory.newInstance(xmlstants.W3C\u XML\u SCHEMA\u NS\u URI);Schema Schema=sf.newSchema(object.getClass().getResource(“/Schema/”+xsdName));。。。它在第二行抛出null…我如何模拟或设置一些值,使它在junit中不抛出null
String path = this.getActivity().getPackageManager().getPackageInfo(getPackageName(), 0).applicationInfo.dataDir;