Android中的XML模式验证-获取;java.lang.ExceptionInInitializerError“;
我找了又找,但我不知道怎么了。我知道标准Android库不支持模式或xml验证,并且您不能使用Apache Xerces开箱即用,因此我使用这个库: 人们似乎对这个图书馆很成功,但我没有。这是我的代码:Android中的XML模式验证-获取;java.lang.ExceptionInInitializerError“;,java,android,xsd,xerces,xml-validation,Java,Android,Xsd,Xerces,Xml Validation,我找了又找,但我不知道怎么了。我知道标准Android库不支持模式或xml验证,并且您不能使用Apache Xerces开箱即用,因此我使用这个库: 人们似乎对这个图书馆很成功,但我没有。这是我的代码: private boolean validateDocument() { boolean result; try { InputStream is = context.getResources().openRawResource(R.raw.xml_sch
private boolean validateDocument() {
boolean result;
try {
InputStream is = context.getResources().openRawResource(R.raw.xml_schema);
File file = context.getFileStreamPath(fileName);
Source schemaFile = new StreamSource(is);
Source xmlFile = new StreamSource(file);
SchemaFactory factory = new XMLSchemaFactory();
Schema schema = factory.newSchema(schemaFile);
Validator validator = schema.newValidator();
validator.validate(xmlFile);
result = true;
} catch (SAXException e) {
result = false;
e.printStackTrace();
} catch (IOException ie) {
result = false;
ie.printStackTrace();
}
return result;
}
我不相信我的xml_模式文件是正确的,但这似乎不是问题所在。无论如何,这里是:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="manifest" type="ManifestType" />
<xs:complexType name="ManifestType" >
<xs:sequence>
<xs:element name="base_url" type="BaseUrl" maxOccurs="1" />
<xs:element name="current_version" type="CurrentVersion" maxOccurs="1" />
<xs:element name="version" type="VersionType" minOccurs="1" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="BaseUrl" >
<xs:attribute name="url" type="xs:string" use="required" />
</xs:complexType>
<xs:complexType name="CurrentVersion" >
<xs:attribute name="id" type="xs:string" use="required" />
<xs:attribute name="zip_url" type="xs:string" use="required" />
<xs:attribute name="build_md5" type="xs:boolean" use="required" />
</xs:complexType >
<xs:complexType name="VersionType" >
<xs:sequence >
<xs:element name="file" type="FileType" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="add" type="AddType" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="remove" type="RemoveType" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
<xs:attribute name="id" type="xs:string" use="required" />
<xs:attribute name="build_md5" type="xs:string" />
<xs:attribute name="prefix" type="xs:string" />
</xs:complexType>
<xs:complexType name="FileType" >
<xs:attribute name="path" type="xs:string" use="required" />
<xs:attribute name="url" type="xs:string" />
<xs:attribute name="md5" type="xs:string" />
<xs:attribute name="patch" type="xs:string" />
<xs:attribute name="file" type="xs:string" use="required" />
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="permissions" use="required" >
<xs:simpleType>
<xs:restriction base="xs:integer" >
<xs:minInclusive value="0" />
<xs:maxInclusive value="777" />
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
<xs:complexType name="AddType" >
<xs:attribute name="path" type="xs:string" use="required" />
<xs:attribute name="url" type="xs:string" />
<xs:attribute name="file" type="xs:string" use="required" />
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="permissions" use="required" >
<xs:simpleType>
<xs:restriction base="xs:integer" >
<xs:minInclusive value="0" />
<xs:maxInclusive value="777" />
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
<xs:complexType name="RemoveType" >
<xs:attribute name="path" type="xs:string" use="required" />
<xs:attribute name="name" type="xs:string" use="required" />
</xs:complexType>
</xs:schema>
我没有一个确切的XML文件可以验证,但下面是我用于测试的示例:
<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<base_url url="https://whatever.com" />
<current_version id="someversion"
zip_url="somedownloadurl.zip"
build_md5="true" />
<version id="versionA" build_md5="md5sum" prefix="rom_update_version=" >
<file path="path/of/example" url="https://someurl.com" md5="md5sum" patch="path/of/example/file.patch" file="path/of/example/file" name="file" permissions="645" />
<file path="path/of/example2" md5="md5sum" patch="path/of/example2/file2.patch" file="path/of/example2/file2" name="file2" permissions="665" />
<add path="path/of/example3" file="path/of/example3/file3" name="file3" permissions="333" />
<remove path="path/of/example4" name="file4" />
</version>
<version id="versionB" build_md5="md5sum" prefix="rom_update_version=" >
<file path="path/of/example" md5="md5sum" patch="path/of/example/file.patch" file="path/of/example/file" name="file" permissions="777" />
<file path="path/of/example2" url="https://someurl.com" md5="md5sum" patch="path/of/example2/file2.patch" file="path/of/example2/file2" name="file2" permissions="775" />
<add path="path/of/example3" file="path/of/example3/file3" name="file3" permissions="333" />
<remove path="path/of/example4" name="file4" />
</version>
</manifest>
以下是完全例外:
Caused by: java.lang.ExceptionInInitializerError
at mf.org.apache.xerces.impl.dv.SchemaDVFactory.getInstance(SchemaDVFactory.java:73)
at mf.org.apache.xerces.impl.dv.SchemaDVFactory.getInstance(SchemaDVFactory.java:55)
at mf.org.apache.xerces.impl.xs.XMLSchemaLoader.reset(XMLSchemaLoader.java:999)
at mf.org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(XMLSchemaLoader.java:536)
at mf.org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(XMLSchemaLoader.java:515)
at mf.org.apache.xerces.jaxp.validation.XMLSchemaFactory.newSchema(XMLSchemaFactory.java:237)
at mf.javax.xml.validation.SchemaFactory.newSchema(SchemaFactory.java:611)
at wav.demon.cognitionupdate.XMLRetriever.XMLParser.validateDocument(XMLParser.java:116)
at wav.demon.cognitionupdate.XMLRetriever.XMLParser.<init>(XMLParser.java:78)
at wav.demon.cognitionupdate.XMLRetriever.TestUpdate.doInBackground(TestUpdate.java:70)
at wav.demon.cognitionupdate.XMLRetriever.TestUpdate.doInBackground(TestUpdate.java:28)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
... 4 more
原因:java.lang.ExceptionInInitializeError
位于mf.org.apache.xerces.impl.dv.SchemaDVFactory.getInstance(SchemaDVFactory.java:73)
位于mf.org.apache.xerces.impl.dv.SchemaDVFactory.getInstance(SchemaDVFactory.java:55)
位于mf.org.apache.xerces.impl.xs.XMLSchemaLoader.reset(XMLSchemaLoader.java:999)
位于mf.org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(XMLSchemaLoader.java:536)
位于mf.org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(XMLSchemaLoader.java:515)
位于mf.org.apache.xerces.jaxp.validation.XMLSchemaFactory.newSchema(XMLSchemaFactory.java:237)
位于mf.javax.xml.validation.SchemaFactory.newSchema(SchemaFactory.java:611)
位于wav.demon.cognitionupdate.XMLRetriever.XMLParser.ValidatedDocument(XMLParser.java:116)
位于wav.demon.cognitionupdate.XMLRetriever.XMLParser。(XMLParser.java:78)
位于wav.demon.cognitionupdate.XMLRetriever.TestUpdate.doInBackground(TestUpdate.java:70)
位于wav.demon.cognitionupdate.XMLRetriever.TestUpdate.doInBackground(TestUpdate.java:28)
在android.os.AsyncTask$2.call(AsyncTask.java:287)
在java.util.concurrent.FutureTask.run(FutureTask.java:234)处
... 4更多
位于wav.demon.cognitionupdate.XMLRetriever.XMLParser.validatedDocument(XMLParser.java:116)
是我的第一段代码,它是Schema=factory.newSchema(schemaFile)代码>行
我一直在寻找这个问题的解决办法,但我就是一窍不通。无论我在哪里,看起来我都做得很好,但这根本不起作用。非常感谢您的帮助。我已经尝试了您的xml数据/xml方案,但它们都无效:
甚至java堆栈也会这样说。您的xml文件无效模式文件或测试xml文件?两者,请检查我的答案。但是您应该马上指出xml是无效的:我明白了,几分钟前我在寻找解决方案时添加了这一行;我没有看到打字错误。我不知道这个网站存在,我会调查它。好的,我编辑了原始帖子以显示我的更改,根据你链接到的网站,xml文件现在是有效的。不过,我收到了完全相同的错误消息。我不知道,我要检查的最后一件事是:你确定你引用了所有的JAR,并将它们添加到构建路径吗?我不确定你的意思,但我使用的是Android Studio,所有内容都被添加并编译良好。你确定吗;Android studio甚至还不是beta版,所以可能缺少一些东西。我想说的是,您直接调用的xerces库可能位于in路径中,但它所依赖的其他库可能不在其中,因此在运行时会出现错误。