Android架构验证

犀渠玉剑良家子,白马金羁侠少年。这篇文章主要讲述Android架构验证相关的知识,希望能为你提供帮助。
我创建了一个XML,我有一个XSD文件,我必须用这个模式验证xml,我能做到这一点的任何一个例子。我必须将xsd文件放在我的项目中,以便我可以使用该模式进行验证。
答案根据documentation javax.xml.validation从API级别8开始支持。
(我会测试并尽快报告)
更新好吧,问题不是那么简单:
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
如上所述here,在8级和9级API上都出现了IllegalArgumentException失败
除了找到完全相同的失败报告之外,Google在此方面没有帮助。 API在这里但不是实现(默认)。
另一答案这是Google here发布的一个已知问题
解决方案是使用移植到android的Apache Xerces。有一个项目here
您必须执行svn checkout并将项目导出到jar文件以用作android proyect中的库。
实例SchemaFactory的代码稍有改动。我给你举个例子:

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; SchemaFactoryfactory = new XMLSchemaFactory(); Schema esquema = factory.newSchema(".../file.xsd");

另一答案这可能没什么帮助,但最后我查了一下,Android平台的Java环境有些局限。主要的问题是,人们期望一些API可用 - 特别是javax.xml.validation(JDK 1.5和anove的一部分!) - 不在那里。
因此,您可能需要包含比在非阉割的Java平台上更多的罐子。此外,由于黑/白名单问题,可能会增加标准API的限制(这是Google AppEngine的一个大问题,而且由于Android早于它,它也有类似的挑战)。
除此之外,我会尝试将javax.xml.validation与捆绑的XML解析器Xerces一起使用。有关如何做到这一点的方法文件的gazillions。
另一答案在阅读了很多帖子并尝试了一堆不同的东西之后,我终于通过Xerces-for-Android让我的工作正常,并试图为其他人很好地记录这个过程...希望它有帮助:)
以下对我有用:
  1. 创建验证实用程序。
  2. 将xml和xsd都放到android OS上的文件中,然后使用验证实用程序。
  3. 使用Xerces-For-Android进行验证。
Android确实支持我们可以使用的一些软件包,我基于:http://docs.oracle.com/javase/1.5.0/docs/api/javax/xml/validation/package-summary.html创建了我的xml验证实用程序
我最初的沙箱测试非常流畅,然后我尝试将其移植到Dalvik并发现我的代码不起作用。有些事情与Dalvik不相同,所以我做了一些修改。
【Android架构验证】我找到了对xerces for android的引用,所以我修改了我的沙箱测试(以下不适用于android,这之后的例子):
import java.io.File; import javax.xml.XMLConstants; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Source; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamSource; import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; import javax.xml.validation.Validator; import org.w3c.dom.Document; /** * A Utility to help with xml communication validation. */ public class XmlUtil {/** * Validation method. * Base code/example from: http://docs.oracle.com/javase/1.5.0/docs/api/javax/xml/validation/package-summary.html * * @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. */ public static boolean validate(String xmlFilePath, String xmlSchemaFilePath) {// parse an XML document into a DOM tree DocumentBuilder parser = null; Document document; // Try the validation, we assume that if there are any issues with the validation // process that the input is invalid. try { // validate the DOM tree parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); document = parser.parse(new File(xmlFilePath)); // create a SchemaFactory capable of understanding WXS schemas SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); // load a WXS schema, represented by a Schema instance Source schemaFile = new StreamSource(new File(xmlSchemaFilePath)); Schema schema = factory.newSchema(schemaFile); // create a Validator instance, which can be used to validate an instance document Validator validator = schema.newValidator(); validator.validate(new DOMSource(document)); } catch (Exception e) { // Catches: SAXException, ParserConfigurationException, and IOException. return false; }return true; } }

上面的代码必须修改一些与xerces for android(http://gc.codehum.com/p/xerces-for-android/)一起使用。您需要SVN才能获得该项目,以下是一些婴儿床注意事项:
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(http://ant.apache.org/manual/using.html)快速制作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 { SchemaFactoryfactory = 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(); } }

    推荐阅读