使用Xerces在Android上针对架构验证XML

时间:2012-04-03 17:13:45

标签: android xml validation schema xerces

因此,在咨询this thread后,javax.xml.validation库在Android上无效,我必须找到另一种解决方案。我已经尝试过使用Xerces API,虽然它似乎对很多人来说都很好,但是我无法让它正常工作。

我正在使用存储在SD卡中的文件中的本地XML架构。

我使用的代码如下:

    public static boolean validate(String XmlDocumentUrl, String SchemaUrl) {
    SAXParser parser = new SAXParser();
    try {

        parser.setFeature("http://xml.org/sax/features/namespaces", true);
        parser.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
        parser.setFeature("http://xml.org/sax/features/validation", true);
        parser.setFeature("http://apache.org/xml/features/validation/schema", true);
        parser.setFeature("http://apache.org/xml/features/standard-uri-conformant", false);
        parser.setProperty(
                "http://apache.org/xml/properties/schema/external-schemaLocation",
                "http://www.topografix.com/GPX/1/0 file:///mnt/sdcard/gpxSchema1.0.xsd");



        Validator handler = new Validator();

        parser.setErrorHandler(handler);
        parser.parse(XmlDocumentUrl);
        if (handler.validationError == true){
            System.out.println("XML Document has Error:"

                    + handler.validationError + ""
                    + handler.saxParseException.getMessage());
        return false;
        }
        else{
            System.out.println("XML Document is valid");
        return true;
        }
    } catch (java.io.IOException ioe) {
        System.out.println("IOException" + ioe.getMessage());
    } catch (SAXException e) {
        System.out.println("SAXException" + e.getMessage());
    }
    return false;
}

private static class Validator extends DefaultHandler {
    public boolean validationError = false;
    public SAXParseException saxParseException = null;

    public void error(SAXParseException exception) throws SAXException {
        validationError = true;
        saxParseException = exception;
    }

    public void fatalError(SAXParseException exception) throws SAXException {
        validationError = true;
        saxParseException = exception;
    }

    public void warning(SAXParseException exception) throws SAXException {
    }
}

通过试验这段代码,我认为这会导致问题:  parser.setProperty( "http://apache.org/xml/properties/schema/external-schemaLocation", "http://www.topografix.com/GPX/1/0 file:///mnt/sdcard/gpxSchema1.0.xsd");

我认为由于某种原因找不到.xsd文件,但我不确定。 如果有人能解释我做错了什么,或者是否有与这个属性无关的东西并且仍然不对,我会很高兴。

我得到的错误如下:

04-03 18:12:05.125: E/AndroidRuntime(20457): FATAL EXCEPTION: main
04-03 18:12:05.125: E/AndroidRuntime(20457): java.lang.***ExceptionInInitializerError***
04-03 18:12:05.125: E/AndroidRuntime(20457):    at org.apache.xerces.parsers.XML11Configuration.configurePipeline(Unknown Source)
04-03 18:12:05.125: E/AndroidRuntime(20457):    at org.apache.xerces.parsers.XIncludeAwareParserConfiguration.configurePipeline(Unknown Source)
04-03 18:12:05.125: E/AndroidRuntime(20457):    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
04-03 18:12:05.125: E/AndroidRuntime(20457):    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
04-03 18:12:05.125: E/AndroidRuntime(20457):    at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
04-03 18:12:05.125: E/AndroidRuntime(20457):    at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
04-03 18:12:05.125: E/AndroidRuntime(20457):    at com.pe60t0.Meche.inputhandling.util.XMLSchemaValidator2.validate(XMLSchemaValidator2.java:29)
04-03 18:12:05.125: E/AndroidRuntime(20457):    at com.pe60t0.Meche.inputhandling.validation.gpx.schemas.StrategyGPXSchema1_0.validate(StrategyGPXSchema1_0.java:11)
04-03 18:12:05.125: E/AndroidRuntime(20457):    at com.pe60t0.Meche.inputhandling.validation.gpx.schemas.GPXSchemaValidatorGeneral.executeStrategy(GPXSchemaValidatorGeneral.java:13)
04-03 18:12:05.125: E/AndroidRuntime(20457):    at com.pe60t0.Meche.inputhandling.validation.gpx.GPXValidator.isValidAgainstSchema(GPXValidator.java:115)
04-03 18:12:05.125: E/AndroidRuntime(20457):    at com.pe60t0.Meche.inputhandling.validation.gpx.GPXValidator.validate(GPXValidator.java:34)
04-03 18:12:05.125: E/AndroidRuntime(20457):    at com.pe60t0.Meche.inputhandling.validation.ValidatorGeneral.executeStrategy(ValidatorGeneral.java:15)
04-03 18:12:05.125: E/AndroidRuntime(20457):    at com.pe60t0.Meche.inputhandling.MecheTrackParser.isValidMecheFile(MecheTrackParser.java:55)
04-03 18:12:05.125: E/AndroidRuntime(20457):    at com.pe60t0.Meche.inputhandling.MecheTrackParser.parse(MecheTrackParser.java:30)
04-03 18:12:05.125: E/AndroidRuntime(20457):    at com.pe60t0.Meche.statePattern.states.InitState.selectFile(InitState.java:33)
04-03 18:12:05.125: E/AndroidRuntime(20457):    at com.pe60t0.Meche.main.MecheModel.selectFile(MecheModel.java:39)
04-03 18:12:05.125: E/AndroidRuntime(20457):    at com.pe60t0.Meche.MecheActivity$1.onClick(MecheActivity.java:118)

3 个答案:

答案 0 :(得分:0)

问题是android没有xerces并验证interface =) 从盒子中BTW xerces ASIS在android中不起作用,因为有些类被排除在最终APK之外,因为它们使用不同的目标进行编译。 但你可以简单地从xerce中添加验证:

你可以在adnroid中使用xerces和本机模式验证(在java中) - 你必须下载xerces源代码并且(在一些简单的操作之后)将它包含在你自己的代码中 - 你将能够使用DocumentBuilderFactory.setShema方法。 / p>

https://stackoverflow.com/questions/13142567/xml-schema-validation-xmlsignature-with-xerces-in-android

答案 1 :(得分:0)

谷歌在http://code.google.com/p/android/issues/detail?id=7395

发布了一个已知问题

解决方案是使用像你说的移植到Android的Apache Xerces。这里有一个proyect http://code.google.com/p/xerces-for-android/

你必须做一个svn chekout并将proyect导出到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;


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

答案 2 :(得分:0)

我发现我无法让常规的xerces与Android一起工作,但我确实找到了Xerces-for-Android,我开始工作了。以下是设置和一些示例代码的详细信息。祝你好运:)

以下对我有用:

  1. 创建验证实用程序。
  2. 在Android操作系统上同时获取xml和xsd文件并对其使用验证实用程序。
  3. 使用Xerces-For-Android进行验证。
  4. Android确实支持我们可以使用的一些软件包,我根据以下内容创建了我的xml验证工具:http://docs.oracle.com/javase/1.5.0/docs/api/javax/xml/validation/package-summary.html

    我最初的沙盒测试使用java非常顺利,然后我尝试将其移植到Dalvik并发现我的代码无效。有些事情并没有得到Dalvik的支持,所以我做了一些修改。

    我找到了对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快速制作jar({{ 3}})),我能够得到以下内容来进行我的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;   
    

    有点hackish,它的确有效。我确信有更简洁的方法可以做到这一点,但是我认为我分享了我的成功,因为我找不到任何好的例子。