Java Android模式验证

Java Android模式验证,java,android,xsd,Java,Android,Xsd,我已经创建了一个XML,我有一个XSD文件,我必须用这个模式验证XML,我可以得到任何这样做的例子吗。我必须将xsd文件放在我的项目中,以便使用该模式进行验证 这可能帮不了什么忙,但我最后检查了一下,Android平台在Java环境中受到了一定的限制。而主要的问题是,一些人们期望可用的API——特别是javax.xml.validation(JDK1.5和anove的一部分!)——并不存在 因此,您可能需要包含比在非阉割java平台上更多的JAR。 此外,由于黑名单/白名单问题(这是谷歌AppE

我已经创建了一个
XML
,我有一个
XSD
文件,我必须用这个模式验证XML,我可以得到任何这样做的例子吗。我必须将xsd文件放在我的项目中,以便使用该模式进行验证

这可能帮不了什么忙,但我最后检查了一下,Android平台在Java环境中受到了一定的限制。而主要的问题是,一些人们期望可用的API——特别是javax.xml.validation(JDK1.5和anove的一部分!)——并不存在

因此,您可能需要包含比在非阉割java平台上更多的JAR。 此外,由于黑名单/白名单问题(这是谷歌AppEngine的一个大问题,而且由于Android早于它,所以它也面临类似的挑战),在添加标准API方面可能存在限制

除此之外,我将尝试将javax.xml.validation与捆绑的xml解析器Xerces一起使用。有无数关于如何做到这一点的操作文档。

根据javax.xml。从API级别8开始,就支持验证

(我将对此进行测试并尽快报告)

更新 好吧,问题不是那么简单:

SchemaFactory factory=SchemaFactory.newInstance(xmlstants.W3C\u XML\u SCHEMA\u NS\u URI)

在级别8和级别9 API上都失败,出现IllegalArgumentException,如所述

谷歌在这件事上没有任何帮助,只是找到了同样的失败报告。
API在这里,但不是实现(默认)。

这是谷歌发布的一个已知问题

解决方案是使用ApacheXerces移植到Android。 有一个项目

您必须执行svn chekout并将项目导出到jar文件,以用作android项目中的库

实例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;

SchemaFactory  factory = new XMLSchemaFactory();
Schema esquema = factory.newSchema(".../file.xsd");

在阅读了许多帖子并尝试了一系列不同的东西之后,我终于在Xerces for Android上正常工作了,并试图为其他人很好地记录这个过程。。。希望有帮助:)

以下几点对我很有用:

  • 创建一个验证实用程序
  • 在android操作系统上将xml和xsd都放入文件中,并对其使用验证实用程序
  • 使用Xerces For Android进行验证
  • Android确实支持一些我们可以使用的软件包,我基于以下内容创建了我的xml验证实用程序:

    我最初使用java进行的沙盒测试非常顺利,然后我尝试将其移植到Dalvik,发现我的代码无法工作。Dalvik不支持同样的东西,所以我做了一些修改

    我找到了一个对xerces for android的引用,因此我修改了我的沙盒测试(以下内容不适用于android,后面的示例适用于):

    上面的代码必须经过一些修改才能与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()快速制作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;   
    
    有点老套,它很管用。我相信有一种更简洁的方法可以做到这一点,但我想我会分享我的成功,因为我没有找到任何好的例子

    String path = this.getActivity().getPackageManager().getPackageInfo(getPackageName(), 0).applicationInfo.dataDir;