在SchemaFactory不支持W3C XML模式的平台级8? [英] SchemaFactory doesn't support W3C XML Schema in platform level 8?

查看:162
本文介绍了在SchemaFactory不支持W3C XML模式的平台级8?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随着Android SDK,以下code在一个普通的空活动失败:

  @覆盖
保护无效的OnStart(){
    super.onStart();

    的SchemaFactory的SchemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
}
 

2.2模拟器logcat中显示了这种异常:

  5月6号至28号:38:06.107:WARN / dalvikvm(495):主题ID = 1:螺纹退出与未捕获的异常(组= 0x4001d800)
5月6日至28日:38:06.128:ERROR / AndroidRuntime(495):致命异常:主要
        java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.example / com.example.HelloWorldActivity}:java.lang.IllegalArgumentException:如果http://www.w3.org/2001/XMLSchema
        在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
        在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
        在android.app.ActivityThread.access $ 2300(ActivityThread.java:125)
        在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033)
        在android.os.Handler.dispatchMessage(Handler.java:99)
        在android.os.Looper.loop(Looper.java:123)
        在android.app.ActivityThread.main(ActivityThread.java:4627)
        在java.lang.reflect.Method.invokeNative(本机方法)
        在java.lang.reflect.Method.invoke(Method.java:521)
        在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
        在dalvik.system.NativeStart.main(本机方法)
        java.lang.IllegalArgumentException异常:产生的原因http://www.w3.org/2001/XMLSchema
        在javax.xml.validation.SchemaFactory.newInstance(SchemaFactory.java:194)
        在com.example.HelloWorldActivity.onStart(HelloWorldActivity.java:26)
        在android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
        在android.app.Activity.performStart(Activity.java:3781)
        在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2636)
        ... 11更多
 

的<一个href="http://developer.android.com/reference/javax/xml/validation/SchemaFactory.html#newInstance(java.lang.String)">Javadoc的SchemaFactory的提到平台默认的SchemaFactory坐落在一个具体实施方式。必须有一个平台默认的SchemaFactory为W3C XML模式。

解决方案

我有同样的问题,看了很多帖子之前,我为我工作的一个答案

。参考到恒定不会上的Dalvik工作。我发现我不得不改变我的code一个Xerces换Android项目,然后我就能够得到XML验证我以后的工作。这是最有可能你在做什么的变量引用。设置以下是详细资料和c展示如何得到验证工作在Android上一些例如$ C $。

下面为我工作:

  1. 创建一个验证工具。
  2. 同时获得了XML和XSD文件到Android操作系统,并使用验证工具反对。
  3. 使用Xerces的换机器人做验证。

安卓不支持一些软件包,我们可以使用,我建立了我的XML验证工具的基础上:的http://docs.oracle.com/javase/1.5.0/docs/api/javax/xml/validation/package-summary.html

我最初的沙盒测试是pretty的用java顺利,然后我试图将它移植过来的Dalvik,发现我的code没有工作。有些东西只是不支持同Dalvik的,所以我做了一些修改。

我发现了一个参考的Xerces为Android,所以我修改了我的沙盒测试(以下不与Android合作,在此之后的例子确实):

进口的java.io.File; 进口javax.xml.XMLConstants; 进口javax.xml.parsers.DocumentBuilder中; 进口javax.xml.parsers.DocumentBuilderFactory中; 进口javax.xml.transform.Source; 进口javax.xml.transform.dom.DOMSource中; 进口javax.xml.transform.stream.StreamSource中; 进口javax.xml.validation.Schema; 进口的javax.xml.validation.SchemaFactory; 进口javax.xml.validation.Validator中; 进口org.w3c.dom.Document中; / **  *一个实用程序来帮助XML通信验证。  * / 公共类XmlUtil {     / **      *验证方法。      *基础code /例如,从:http://docs.oracle.com/javase/1.5.0/docs/api/javax/xml/validation/package-summary.html      *      *参数xmlFilePath我们试图验证XML文件。      *参数xmlSchemaFilePath我们使用用于验证的架构文件。这种方法假定模式文件是否有效。      * @返回真,如果有效,假,如果无效,或者坏的解析。      * /     公共静态布尔验证(字符串xmlFilePath,字符串xmlSchemaFilePath){         //解析XML文档转换为DOM树         DocumentBuilder的解析器= NULL;         文档文件;         //尝试验证,我们假设,如果有与审定的任何问题         //过程,输入无效。         尝试 {             //验证DOM树             解析器= DocumentBuilderFactory.newInstance()newDocumentBuilder()。             文档= parser.parse(新文件(xmlFilePath));             //创建能够了解WXS模式一的SchemaFactory             SchemaFactory的工厂= SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);             //加载一个Schema实例psented一个WXS模式,重新$ P $             来源schemaFile =新StreamSource的(新文件(xmlSchemaFilePath));             架构模式= factory.newSchema(schemaFile);             //创建一个验证器实例,它可以用来验证实例文档             验证验证= schema.newValidator();             validator.validate(新DOMSource的(文件));         }赶上(例外五){             //捕捞量:SAXException中,的ParserConfigurationException和IOException异常。             返回false;         }         返回true;     } }

以上code已经被修改了一些使用Xerces为Android(的 HTTP://gc.$c$chum.com/p/xerces-for-android/ )。你需要SVN拿到项目,以下是一些婴儿床注意事项:

 下载Xerces的换机器人
    从http://www.sliksvn.com/en/download下载丝绸SVN(适用于Windows用户)
        安装丝绸SVN(我没有完全安装)
        一旦安装完成,你应该拥有svn在你的系统路径。
        通过在命令行中键入SVN测试。
        我去了我的桌面,然后通过下载的Xerces的项目:
            svn签HTTP://xerces-for-android.google$c$c.com/svn/trunk/的Xerces换安卓只读
        那么你应该有一个新的文件夹在桌面上呼吁的Xerces换安卓只读
 

通过上面的罐子(最后,我会让它变成一个罐子,只需直接复制到我的源代码,以便快速测试。如果你想这样做,你可以用Ant使罐子快( http://ant.apache.org/manual/using.html )),我是能够得到下面的工作,我的XML验证:

进口的java.io.File; 进口java.io.IOException异常; 进口mf.javax.xml.transform.Source; 进口mf.javax.xml.transform.stream.StreamSource; 进口mf.javax.xml.validation.Schema; 进口mf.javax.xml.validation.SchemaFactory; 进口mf.javax.xml.validation.Validator; 进口mf.org.apache.xerces.jaxp.validation.XMLSchemaFactory; 进口org.xml.sax.SAXException; / **  *一个实用程序来帮助XML通信验证。  * / public类XmlUtil {     / **      *验证方法。      *      *参数xmlFilePath我们试图验证XML文件。      *参数xmlSchemaFilePath我们使用用于验证的架构文件。这种方法假定模式文件是否有效。      * @返回真,如果有效,假,如果解析过程中无效或坏的解析或异常/错误。      * /     公共静态布尔验证(字符串xmlFilePath,字符串xmlSchemaFilePath){         //尝试验证,我们假设,如果有与审定的任何问题         //过程,输入无效。         尝试 {             SchemaFactory的工厂=新XMLSchemaFactory();             来源schemaFile =新StreamSource的(新文件(xmlSchemaFilePath));             来源xmlSource =新StreamSource的(新文件(xmlFilePath));             架构模式= factory.newSchema(schemaFile);             验证验证= schema.newValidator();             validator.validate(xmlSource);         }赶上(的SAXException E){             返回false;         }赶上(IOException异常E){             返回false;         }赶上(例外五){             //渔获一切超越:SAXException中,和IOException异常。             e.printStackTrace();             返回false;         }赶上(误差e){             //需要这种,因为我在和我第一次的code组问题时,调试。             e.printStackTrace();             返回false;         }         返回true;     } }

一些副作用注:

有关创建的文件,我做了一个简单的文件工具写字符串的文件:

公共静态无效createFileFromString(字符串FILETEXT,字符串文件名){     尝试 {         档案文件=新的文件(文件名);         BufferedWriter将输出=新的BufferedWriter(新的FileWriter(文件));         output.write(FILETEXT);         output.close();     }赶上(IOException异常E){        e.printStackTrace();     } }

我还需要写,我接触到的领域,所以我利用了:

字符串路径= this.getActivity()。getPackageManager()。getPackageInfo(getPackageName(),0).applicationInfo.dataDir ;

有一个小的hackish,它的工作原理。我敢肯定有这样做的更简洁的方式,但我想我会分享我的成功,因为有没有,我发现任何很好的例子。

With the Android SDK, the following code in a plain empty Activity fails:

@Override
protected void onStart() {
    super.onStart();

    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
}

The 2.2 emulator logcat shows this exception:

06-28 05:38:06.107: WARN/dalvikvm(495): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-28 05:38:06.128: ERROR/AndroidRuntime(495): FATAL EXCEPTION: main
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.HelloWorldActivity}: java.lang.IllegalArgumentException: http://www.w3.org/2001/XMLSchema
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
        at android.app.ActivityThread.access$2300(ActivityThread.java:125)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:123)
        at android.app.ActivityThread.main(ActivityThread.java:4627)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:521)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.IllegalArgumentException: http://www.w3.org/2001/XMLSchema
        at javax.xml.validation.SchemaFactory.newInstance(SchemaFactory.java:194)
        at com.example.HelloWorldActivity.onStart(HelloWorldActivity.java:26)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
        at android.app.Activity.performStart(Activity.java:3781)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2636)
        ... 11 more

The Javadoc of SchemaFactory mentions "Platform default SchemaFactory is located in a implementation specific way. There must be a platform default SchemaFactory for W3C XML Schema."

解决方案

I had the same problem and read many posts before I got an answer that worked for me. The reference to the constant won't work on Dalvik. I found I had to modify my code to work with the Xerces-for-Android project then I was able to get the xml validation I was after. Which is most likely what you are doing by the variable reference. The following are details of the setup and some example code showing how to get the validation to work on android.

The following worked for me:

  1. Create a validation utility.
  2. Get both the xml and xsd into file on the android OS and use the validation utility against it.
  3. Use Xerces-For-Android to do the validation.

Android does support some packages which we can use, I created my xml validation utility based on: http://docs.oracle.com/javase/1.5.0/docs/api/javax/xml/validation/package-summary.html

My initial sandbox testing was pretty smooth with java, then I tried to port it over to Dalvik and found that my code did not work. Some things just aren't supported the same with Dalvik, so I made some modifications.

I found a reference to xerces for android, so I modified my sandbox test of (the following doesn't work with android, the example after this does):

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;
    }
}

The above code had to be modified some to work with xerces for android (http://gc.codehum.com/p/xerces-for-android/). You need SVN to get the project, the following are some crib notes:

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

With the above jar (Eventually I'll make it into a jar, just copied it directly into my source for quick testing. If you wish to do the same, you can making the jar quickly with Ant (http://ant.apache.org/manual/using.html)), I was able to get the following to work for my xml validation:

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;
    }
}

Some Side Notes:

For creating the files, I made a simple file utility to write string to files:

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();
    }
}

I also needed to write to an area that I had access to, so I made use of:

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

A little hackish, it works. I'm sure there is a more succinct way of doing this, however I figured I'd share my success, as there weren't any good examples that I found.

这篇关于在SchemaFactory不支持W3C XML模式的平台级8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆