我搜索和我已經搜查,但我不知道什麼是錯的。我知道標準的Android庫不支持Schema或xml驗證,並且您不能使用開箱即用的Apache Xerces,因此我正在使用此庫: https://code.google.com/p/xerces-for-android/XML Schema驗證Android中 - 讓「java.lang.ExceptionInInitializerError」
人們似乎對此有成功圖書館,但我沒有。這是我的代碼:
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_schema文件是否正確,但似乎沒有問題。在任何情況下,在這裏它是:
<?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
at wav.demon.cognitionupdate.XMLRetriever.XMLParser.validateDocument(XMLParser.java:116)
是第一個我的代碼,並且它是Schema schema = factory.newSchema(schemaFile);
線。
我已經搜查,搜查,搜查爲解決這個問題,但我只是無能。我看到的每一處看起來都像是在做正確的事情,但這只是行不通的。任何幫助將不勝感激。
你的XML文件無效 – Marco
架構文件或測試xml文件? – DemonWav
兩個,檢查我的答案。但你應該有標記馬上XML是無效的:<清單的xmlns:XSI = 「http://www.w3.org/2001/XMLSchema-instance」 XSI = 「noNameSpaceSchemaLocation =」 xml_schema.xsd」> – Marco