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
Java Applet:XML可以';找不到DTD_Java_Xml_Applet_Dtd - Fatal编程技术网

Java Applet:XML可以';找不到DTD

Java Applet:XML可以';找不到DTD,java,xml,applet,dtd,Java,Xml,Applet,Dtd,我正在编写一个小程序,它使用DTD文件检查它接收的XML的内容 我的问题是DTD没有放在applet viewer的正确文件夹中,但是现在我在服务器上测试它,我再次遇到同样的错误 java.security.AccessControlException: access denied (java.io.FilePermission/leveldtd.dtd read) 当小程序位于服务器上时,如何修复此问题 XML解析器创建。对于要从服务器获取资源的小程序,它必须使用URL。文件对象

我正在编写一个小程序,它使用DTD文件检查它接收的XML的内容

我的问题是DTD没有放在applet viewer的正确文件夹中,但是现在我在服务器上测试它,我再次遇到同样的错误

java.security.AccessControlException: 
    access denied (java.io.FilePermission/leveldtd.dtd read)
当小程序位于服务器上时,如何修复此问题



XML解析器创建。

对于要从服务器获取资源的小程序,它必须使用URL。文件对象将无法工作,因为:

  • 文件
    对象将指向用户计算机上的一个位置。
  • 它需要一个受信任的小程序来使用
    文件
    对象。因此,输出中的
    AccessControlException
  • 使用
    URL(baseURL,pathString)
    构造函数可以很容易地形成指向资源的URL,其中基本URL是从
    Applet.getDocumentBase()或
    Applet.getCodeBase()获取的

    …如何将URL提供给解析器

    下面是一段代码片段,它使用一个JAR中的XSD。URL存储在
    schemaSource

    try {
        URL schemaSource = Thread.currentThread().getContextClassLoader().getResource("JNLP-6.0.xsd");
        System.out.println( "schemaSource: " + schemaSource );
    
        DocumentBuilderFactory factory =
            DocumentBuilderFactory.newInstance();
        factory.setFeature("http://xml.org/sax/features/validation", true);
        factory.setFeature("http://apache.org/xml/features/validation/schema", true) ;
        factory.setFeature("http://xml.org/sax/features/namespaces", true) ;
        factory.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
        factory.setAttribute(
            "http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
            schemaSource.toString());
        factory.setNamespaceAware(true);
        factory.setValidating(true);
    
        InputStream schemaStream = schemaSource.openStream();
        try {
            StreamSource ss = new StreamSource( schemaStream );
            String language = XMLConstants.W3C_XML_SCHEMA_NS_URI;
            SchemaFactory schemaFactory = SchemaFactory.newInstance(language);
    
            Schema schema = schemaFactory.newSchema(ss);
            factory.setSchema( schema );
        }
        finally {
            schemaStream.close();
        }
    
        DocumentBuilder documentBuilder = factory.newDocumentBuilder();
        documentBuilder.setErrorHandler( errorHandler );
    
        InputStream is = page.openStream();
        try {
            document = documentBuilder.parse( is );
        }
        finally {
            is.close();
        }
    
        List<LaunchError> parseErrors = errorHandler.getParseErrors();
        xmlValid = parseErrors.isEmpty();
        errors.addAll(parseErrors);
    } catch(Exception e) {
        System.err.println( "Error: " + e.getMessage() );
        // TODO Show to user
    }
    
    试试看{
    URL schemaSource=Thread.currentThread().getContextClassLoader().getResource(“JNLP-6.0.xsd”);
    System.out.println(“schemaSource:+schemaSource”);
    文档建设者工厂=
    DocumentBuilderFactory.newInstance();
    factory.setFeature(“http://xml.org/sax/features/validation“,对);
    factory.setFeature(“http://apache.org/xml/features/validation/schema“,对);
    factory.setFeature(“http://xml.org/sax/features/namespaces“,对);
    factory.setFeature(“http://apache.org/xml/features/validation/schema-full-checking“,对);
    factory.setAttribute(
    "http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
    schemaSource.toString());
    factory.setNamespaceAware(true);
    工厂设置验证(true);
    InputStream schemaStream=schemaSource.openStream();
    试一试{
    StreamSource ss=新的StreamSource(schemaStream);
    String language=xmlstants.W3C\u XML\u SCHEMA\u NS\u URI;
    SchemaFactory SchemaFactory=SchemaFactory.newInstance(语言);
    Schema=schemaFactory.newSchema(ss);
    工厂设置模式(模式);
    }
    最后{
    schemaStream.close();
    }
    DocumentBuilder DocumentBuilder=factory.newDocumentBuilder();
    documentBuilder.setErrorHandler(errorHandler);
    InputStream=page.openStream();
    试一试{
    document=documentBuilder.parse(is);
    }
    最后{
    is.close();
    }
    List parseErrors=errorHandler.getParseErrors();
    xmlValid=parseErrors.isEmpty();
    errors.addAll(parseErrors);
    }捕获(例外e){
    System.err.println(“错误:+e.getMessage());
    //TODO显示给用户
    }
    
    BTW-您如何调用小程序查看器?从IDE?蚂蚁发射?命令行?另外:您使用的是哪个JDK版本(或者更准确地说,applet查看器来自哪个版本)?很抱歉,这听起来可能很愚蠢,但是如何将URL提供给解析器?upil现在解析器会读取xml中有dtd,并会查找它,这样我就不需要对dtd@Jason:你必须要说得更具体些。假装我看不到你的代码。例如,解析器的JavaDocs在哪里?:P很抱歉,我在帖子中添加了一些代码,请告诉我您还有什么需要改进的地方need@Jason:查看对我的问题的编辑。如果这不能解决您的问题,我建议您开始一个与“使用来自URL的DTD验证XML”相关的新问题。
    try {
        URL schemaSource = Thread.currentThread().getContextClassLoader().getResource("JNLP-6.0.xsd");
        System.out.println( "schemaSource: " + schemaSource );
    
        DocumentBuilderFactory factory =
            DocumentBuilderFactory.newInstance();
        factory.setFeature("http://xml.org/sax/features/validation", true);
        factory.setFeature("http://apache.org/xml/features/validation/schema", true) ;
        factory.setFeature("http://xml.org/sax/features/namespaces", true) ;
        factory.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
        factory.setAttribute(
            "http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
            schemaSource.toString());
        factory.setNamespaceAware(true);
        factory.setValidating(true);
    
        InputStream schemaStream = schemaSource.openStream();
        try {
            StreamSource ss = new StreamSource( schemaStream );
            String language = XMLConstants.W3C_XML_SCHEMA_NS_URI;
            SchemaFactory schemaFactory = SchemaFactory.newInstance(language);
    
            Schema schema = schemaFactory.newSchema(ss);
            factory.setSchema( schema );
        }
        finally {
            schemaStream.close();
        }
    
        DocumentBuilder documentBuilder = factory.newDocumentBuilder();
        documentBuilder.setErrorHandler( errorHandler );
    
        InputStream is = page.openStream();
        try {
            document = documentBuilder.parse( is );
        }
        finally {
            is.close();
        }
    
        List<LaunchError> parseErrors = errorHandler.getParseErrors();
        xmlValid = parseErrors.isEmpty();
        errors.addAll(parseErrors);
    } catch(Exception e) {
        System.err.println( "Error: " + e.getMessage() );
        // TODO Show to user
    }