Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
针对XSD模式的Java XML验证_Java_Xml_Validation_Xsd - Fatal编程技术网

针对XSD模式的Java XML验证

针对XSD模式的Java XML验证,java,xml,validation,xsd,Java,Xml,Validation,Xsd,但这会返回一个错误,即: 线程“main”java.lang.IllegalArgumentException中出现异常:无法加载实现由:-实例指定的架构语言的SchemaFactory 这是我的代码还是实际的xsd文件的问题?该错误意味着您安装的Java没有任何可以解析XMLSchema文件的类,因此架构或代码没有问题 我很确定最近的JRE默认有合适的类,所以你能给我们提供java-version的输出吗 更新: 您使用了错误的XMLContants字符串。您需要:xmlcants.W3C\

但这会返回一个错误,即: 线程“main”java.lang.IllegalArgumentException中出现异常:无法加载实现由:-实例指定的架构语言的SchemaFactory


这是我的代码还是实际的xsd文件的问题?

该错误意味着您安装的Java没有任何可以解析XMLSchema文件的类,因此架构或代码没有问题

我很确定最近的JRE默认有合适的类,所以你能给我们提供
java-version
的输出吗


更新:


您使用了错误的XMLContants字符串。您需要:
xmlcants.W3C\u XML\u SCHEMA\u NS\u URI

这些文件基于底层系统。我在为Android编程时也遇到了同样的问题。我发现我必须使用Xerces for Android来解决我的问题

以下内容对我在Android上的验证非常有用,如果您的代码与Android相关,可能会有所帮助,如果不相关,则该方法可能会对您的底层系统有所帮助:

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

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

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

    上面的代码必须经过一些修改才能与xerces for android()配合使用。您需要SVN才能获得该项目,以下是一些提示:

    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;
        }
    }
    
    使用上面的jar(最终我会将其制作成一个jar,直接复制到我的源代码中进行快速测试。如果您希望这样做,可以使用Ant()快速制作jar),我能够获得以下用于xml验证的内容:

    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
    
    一些旁注:

    为了创建文件,我制作了一个简单的文件实用程序来向文件写入字符串:

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

    有点老套,它很管用。我确信有一种更简洁的方法可以做到这一点,但是我想我会分享我的成功,因为我没有找到任何好的例子。

    java版本“1.6.0_17”java(TM)SE运行时环境(构建1.6.0_17-b04)java热点(TM)客户端VM(构建14.3-b01,混合模式,共享)我有同样的问题,但我的系统java版本“1.6.0_20”OpenJDK运行时环境(IcedTea6 1.9.13)(6b20-1.9.13-0ubuntu1~10.10.1)OpenJDK 64位服务器虚拟机(构建19.0-b09,混合模式),我使用XMLConstants.W3C_XML_SCHEMA_NS_URI常量,但我仍然得到上面的错误(java.lang.illegargumentException)。请建议
    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;